I cannot define variables in python named as "x" due to conflict with gates of similar names. I think we should be able to let users use the names.
#import library for drawing quantum circuit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import QuantumProgram
from qiskit.backends import discover_local_backends, discover_remote_backends
from qiskit.tools.visualization import circuit_drawer
x = QuantumRegister("x", 6)
c = ClassicalRegister("c", 1) #classical registers is used as dummy
qc = QuantumCircuit(x, c)
qc.x(x[2])
qc.cx(x[1], x[5])
qc.cx(x[2], x[5])
qc.cx(x[3], x[5])
qc.ccx(x[1], x[2], x[4])
qc.ccx(x[3], x[4], x[5])
qc.ccx(x[1], x[2], x[4])
circuit_drawer(qc)
Running the above code results in QASM error:
QasmError Traceback (most recent call last)
<ipython-input-2-3eec3d24b52c> in <module>()
15 qc.ccx(x[3], x[4], x[5])
16 qc.ccx(x[1], x[2], x[4])
---> 17 circuit_drawer(qc)
~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/tools/visualization.py in circuit_drawer(circuit, basis, scale)
714 with tempfile.TemporaryDirectory() as tmpdirname:
715 latex_drawer(circuit, filename=os.path.join(tmpdirname, filename + '.tex'),
--> 716 basis=basis, scale=scale)
717 im = None
718 try:
~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/tools/visualization.py in latex_drawer(circuit, filename, basis, scale)
785 str: Latex string appropriate for writing to file.
786 """
--> 787 ast = qasm.Qasm(data=circuit.qasm()).parse()
788 if basis:
789 # Split basis only if it is not the empty string.
~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/qasm/_qasm.py in parse(self)
57 with QasmParser(self._filename) as qasm_p:
58 qasm_p.parse_debug(False)
---> 59 return qasm_p.parse(self._data)
~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/qasm/_qasmparser.py in parse(self, data)
1073 def parse(self, data):
1074 """Parse some data."""
-> 1075 self.parser.parse(data, lexer=self.lexer, debug=self.parse_deb)
1076 if self.qasm is None:
1077 raise QasmError("Uncaught exception in parser; "
~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/ply/yacc.py in parse(self, input, lexer, debug, tracking, tokenfunc)
329 return self.parseopt(input, lexer, debug, tracking, tokenfunc)
330 else:
--> 331 return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
332
333
~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/ply/yacc.py in parseopt_notrack(self, input, lexer, debug, tracking, tokenfunc)
1116 del symstack[-plen:]
1117 self.state = state
-> 1118 p.callable(pslice)
1119 del statestack[-plen:]
1120 symstack.append(sym)
~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/qasm/_qasmparser.py in p_qreg_decl(self, program)
478 if program[2].index == 0:
479 raise QasmError("QREG size must be positive")
--> 480 self.update_symtab(program[0])
481
482 def p_qreg_decl_e(self, program):
~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/qasm/_qasmparser.py in update_symtab(self, obj)
81 + ', file', obj.file
82 + '.\nPrevious occurence at line',
---> 83 str(prev.line) + ', file', prev.file)
84 self.current_symtab[obj.name] = obj
85
QasmError: "Duplicate declaration for qreg 'x' at line 3, file .\nPrevious occurence at line 22, file /Users/rraymondhp/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/qasm/libs/qelib1.inc"
I have no ideas
1.
2.
3.
4.
I'm gonna say this is not a bug, and necessary for correct parsing of OpenQASM. A parser builds a symbol table during parsing, and these symbols cannot clash. In OpenQASM, certain string tokens are keywords, like the "x" gate. For that reason, you cannot reuse that keyword in other places. Similar to how in C, you can't name a variable in "int".
@diego-plan9 @atilag @1ucian0 what do you guys think?
I agree it鈥檚 not a bug. Better documentation and error handling is needed in the future.
Most helpful comment
I agree it鈥檚 not a bug. Better documentation and error handling is needed in the future.