- Notifications
You must be signed in to change notification settings - Fork 758
Description
Expected behavior
openqasm 3 defines gphase without any qubit parameters
gphase(0.25); whereas pennylane adds a hard-coded qubit index afterwards.
gphase(0.25) q[0]; This is issue number one, and based on what I tried via qiskit, gphase only works when we use stdgates.inc as opposed to qelib1.inc. Given QASM is meant to be a common IR for every framework, this inconsistency is a major issue.
Furthermore, this is not even digestible by PennyLane itself, as the following snippet fails
import pennylane as qml device = qml.device("default.qubit", wires=2) @qml.set_shots(1000) @qml.qnode(device) def circuit(): qml.Hadamard(wires=0) qml.CNOT(wires=[0, 1]) qml.GlobalPhase(1/4, wires=0) return qml.expval(qml.PauliZ(wires=0)), qml.expval(qml.PauliZ(wires=1)) loaded_circuit = qml.from_qasm3(qml.to_openqasm(circuit)())()NotImplementedError: An unsupported QASM instruction Include was encountered on line 2, in global. and with qml.from_qasm
QASM2ParseError: "<input>:7,0: 'gphase' is not defined in this scope" Actual behavior
gphasemust be consistent with openqasm 3 specification. Custom syntax especially in the absence of gate definition is not acceptable.- If (1) is not considered a priority, then it should at least be compatible within PennyLane itself.
Additional information
I believe it's worthwhile to view:
https://openqasm.com/language/gates.html#gphase
Qiskit itself also seems to have issues with global phase gate, as it produces
from qiskit import QuantumCircuit from qiskit.circuit.library import GlobalPhaseGate from qiskit.qasm3 import dumps qc = QuantumCircuit(2) qc.h(0) qc.cx(0, 1) # Writing to `qc.global_phase` doesn't work, which is fair since # it can just be set to some value as opposed to changes via positive # and negative angles # Although, my preference would have been for it to just add `qc.global_phase` # at the very end if it's non-zero, and have `GlobalPhaseGate` literally just add or # decrease from the property qc.append(GlobalPhaseGate(0.25)) qasm = dumps(qc) print(qasm)OPENQASM 3.0; include "stdgates.inc"; gate global_phase(p0) { } qubit[2] q; h q[0]; cx q[0], q[1]; global_phase(0.25) ; which is not digestible by it
from qiskit.qasm3 import loads qc = loads(qasm)Full traceback
``` --------------------------------------------------------------------------- ParseCancellationException Traceback (most recent call last) File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass\.venv\Lib\site-packages\openqasm3\parser.py:105, in parse(input_, permissive) 104 try: --> [105](file:///C:/Users/A.C.EA/Documents/GitHub/ucc_new_pass/.venv/Lib/site-packages/openqasm3/parser.py:105) tree = parser.program() 106 except (RecognitionException, ParseCancellationException) as exc:File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\openqasm3_antlr_4_13\qasm3Parser.py:659, in qasm3Parser.program(self)
658 self.state = 135
--> 659 self.statementOrScope()
660 self.state = 140
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\openqasm3_antlr_4_13\qasm3Parser.py:1301, in qasm3Parser.statementOrScope(self)
1300 self.state = 203
-> 1301 self.statement()
1302 pass
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\openqasm3_antlr_4_13\qasm3Parser.py:1007, in qasm3Parser.statement(self)
1006 self.state = 172
-> 1007 self.gateStatement()
1008 pass
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\openqasm3_antlr_4_13\qasm3Parser.py:3334, in qasm3Parser.gateStatement(self)
3333 self.state = 430
-> 3334 localctx.qubits = self.identifierList()
3335 self.state = 431
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\openqasm3_antlr_4_13\qasm3Parser.py:6745, in qasm3Parser.identifierList(self)
6744 self.state = 792
-> 6745 self.match(qasm3Parser.Identifier)
6746 self.state = 797
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\antlr4\Parser.py:126, in Parser.match(self, ttype)
125 else:
--> 126 t = self._errHandler.recoverInline(self)
127 if self.buildParseTrees and t.tokenIndex==-1:
128 # we must have conjured up a new token during single token insertion
129 # if it's not the current symbol
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\antlr4\error\ErrorStrategy.py:703, in BailErrorStrategy.recoverInline(self, recognizer)
702 def recoverInline(self, recognizer:Parser):
--> 703 self.recover(recognizer, InputMismatchException(recognizer))
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\antlr4\error\ErrorStrategy.py:697, in BailErrorStrategy.recover(self, recognizer, e)
696 context = context.parentCtx
--> 697 raise ParseCancellationException(e)
ParseCancellationException: None
The above exception was the direct cause of the following exception:
QASM3ParsingError Traceback (most recent call last)
Cell In[16], line 1
----> 1 loads(qasm)
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\qiskit\utils\lazy_tester.py:149, in LazyDependencyManager.require_in_call..decorator..out(*args, **kwargs)
146 @functools.wraps(function)
147 def out(*args, **kwargs):
148 self.require_now(feature)
--> 149 return function(*args, **kwargs)
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\qiskit\qasm3_init_.py:369, in loads(program, annotation_handlers)
367 kwargs["annotation_handlers"] = annotation_handlers
368 try:
--> 369 return qiskit_qasm3_import.parse(program, **kwargs)
370 except qiskit_qasm3_import.ConversionError as exc:
371 raise QASM3ImporterError(str(exc)) from exc
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\qiskit_qasm3_import\api.py:59, in parse(string, annotation_handlers)
43 def parse(
44 string: str,
45 /,
46 *,
47 annotation_handlers: dict[str, annotation.OpenQASM3Serializer] | None = None,
48 ) -> QuantumCircuit:
49 """Wrapper around :func:.convert, which first parses the OpenQASM 3 program into AST form, and
50 then converts the output to Qiskit format.
51
(...) 57 The annotation_handlers parameter.
58 """
---> 59 return convert(openqasm3.parse(string), annotation_handlers=annotation_handlers)
File c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass.venv\Lib\site-packages\openqasm3\parser.py:107, in parse(input_, permissive)
105 tree = parser.program()
106 except (RecognitionException, ParseCancellationException) as exc:
--> 107 raise QASM3ParsingError() from exc
108 return QASMNodeVisitor().visitProgram(tree)
QASM3ParsingError:
</details> and only seems to be okay with this OPENQASM 3.0;
include "stdgates.inc";
qreg q[2];
h q[0];
cx q[0], q[1];
gphase(0.25);
So, I'll have to open a similar issue there as well. ### Source code ```shell Tracebacks
System information
Name: pennylane Version: 0.44.0 Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network. Home-page: Author: License: Location: c:\Users\A.C.EA\Documents\GitHub\ucc_new_pass\.venv\Lib\site-packages Platform info: Windows-10-10.0.26200-SP0 Python version: 3.11.9 Numpy version: 2.3.3 Scipy version: 1.16.2 JAX version: 0.6.0 Installed devices: - default.clifford (pennylane-0.44.0) - default.gaussian (pennylane-0.44.0) - default.mixed (pennylane-0.44.0) - default.qubit (pennylane-0.44.0) - default.qutrit (pennylane-0.44.0) - default.qutrit.mixed (pennylane-0.44.0) - default.tensor (pennylane-0.44.0) - null.qubit (pennylane-0.44.0) - reference.qubit (pennylane-0.44.0) - lightning.qubit (pennylane_lightning-0.44.0) - qiskit.aer (PennyLane-qiskit-0.44.1) - qiskit.basicaer (PennyLane-qiskit-0.44.1) - qiskit.basicsim (PennyLane-qiskit-0.44.1) - qiskit.remote (PennyLane-qiskit-0.44.1)Existing GitHub issues
- I have searched existing GitHub issues to make sure the issue does not already exist.