Problems with global phase
See original GitHub issueInformation
- Qiskit Terra version: 0.18.3
- Python version: Any
- Operating system: Any
What is the current behavior?
If the Optimize1qGatesDecomposition
optimization pass is applied to the sequence
qc = QuantumCircuit(1)
qc.x(0)
qc.p(np.pi/2, 0)
a global phase is introduced in the circuit. The old Optimize1qGates
optimization pass does not introduce such a global phase. Is this intended? If not, then the following problem might rather be a separate issue.
Upon dumping a QASM representation of the circuit via qc.qasm()
the information about the global phase is lost. This means once the resulting file is used to create a circuit via QuantumCircuit.from_qasm_str(qasm_str)
, both circuits are no longer functionally equivalent, but differ by the global phase.
Steps to reproduce the problem
from qiskit import QuantumCircuit, Aer, execute
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes.optimization import Optimize1qGatesDecomposition, Optimize1qGates
backend = Aer.get_backend('aer_simulator')
qc = QuantumCircuit(1)
qc.x(0)
qc.p(np.pi/2, 0)
qc_opt = PassManager(Optimize1qGatesDecomposition(basis=['u1', 'u2', 'u3', 'cx'])).run(qc)
qasm_str = qc_opt.qasm()
print('Optimize1qGatesDecomposition global phase: ', qc_opt.global_phase)
qc_opt2 = PassManager(Optimize1qGates(basis=['u1', 'u2', 'u3', 'cx'])).run(qc)
qasm_str2 = qc_opt2.qasm()
print('Optimize1qGates global phase: ', qc_opt2.global_phase)
qc.save_unitary()
unitary = execute(qc, backend).result().get_unitary(qc)
qc_opt.save_unitary()
unitary_opt = execute(qc_opt, backend).result().get_unitary(qc_opt)
qc_qasm = QuantumCircuit.from_qasm_str(qasm_str)
qc_qasm.save_unitary()
unitary_qasm = execute(qc_qasm, backend).result().get_unitary(qc_qasm)
qc_qasm2 = QuantumCircuit.from_qasm_str(qasm_str2)
qc_qasm2.save_unitary()
unitary_qasm2 = execute(qc_qasm2, backend).result().get_unitary(qc_qasm2)
print(unitary == unitary_opt)
print(unitary == unitary_qasm)
print(unitary == unitary_qasm2)
yields
Optimize1qGatesDecomposition global phase: 5.497787143782138
Optimize1qGates global phase: 0
[[ True True]
[ True True]]
[[False False]
[False False]]
[[ True True]
[ True True]]
What is the expected behavior?
For once, I would have not expected the optimization pass to introduce a global phase. In addition, dumping the circuit to QASM should not alter the circuit’s functionality.
Suggested solutions
Potentially fix the global phase issue in the Optimize1qGatesDecomposition
optimization pass.
Factor in global phase in QASM dump.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
I will add on to @jakelishman’s response here that if you need a lossless serialization format for your circuit for just usage with Qiskit, qpy is a good option: https://qiskit.org/documentation/apidoc/qpy.html
I think it would be a useful enhancement for qasm exporter, to optionally emit the global phase gate even in qasm 2 mode. It could be defined using the gate definition @burgholzer suggested above.