Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect optimization for Qiskit circuit? #134

Closed
bichselb opened this issue Aug 23, 2023 · 2 comments · Fixed by #135
Closed

Incorrect optimization for Qiskit circuit? #134

bichselb opened this issue Aug 23, 2023 · 2 comments · Fixed by #135
Assignees
Labels
bug Something isn't working

Comments

@bichselb
Copy link

bichselb commented Aug 23, 2023

Optimizing a simple Qiskit circuit seems to yield a circuit implementing a different unitary.

Here is a minimal example:

import pyzx as zx
from qiskit.circuit import QuantumCircuit

# version
print('Version:', zx.__version__)

# create simple circuit
circuit_qiskit = QuantumCircuit(1)
circuit_qiskit.t(0)
circuit_qiskit.t(0)
circuit_qiskit.t(0)
circuit_qiskit.t(0)
circuit_qiskit.t(0)
circuit = zx.Circuit.from_qasm(circuit_qiskit.qasm())

# optimize
basic = circuit.to_basic_gates()
optimized = zx.optimize.basic_optimization(basic)
optimized_qiskit = QuantumCircuit.from_qasm_str(optimized.to_qasm())

# draw
print('Before optimization:')
print(circuit_qiskit)
# zx.draw(circuit)
print('After optimization:')
# zx.draw(optimized)
print(optimized_qiskit)

Here is the output:

Version: 0.7.3
Before optimization:
   ┌───┐┌───┐┌───┐┌───┐┌───┐
q: ┤ T ├┤ T ├┤ T ├┤ T ├┤ T ├
   └───┘└───┘└───┘└───┘└───┘
After optimization:
   ┌───┐
q: ┤ T ├
   └───┘

Am I doing something wrong?

@jvdwetering
Copy link
Collaborator

That is definitely a bug in basic_optimization. The problem is that it is combining the phases into a single gate, but not updating the name. The one 'T' gate that is in the circuit optimized actually has a phase parameter of 5pi/4, but it didn't change the name of the phase gate. If you were to call zx.draw(optimized) you would see that it is still holds the correct phase.

Thanks for reporting, I'm surprised such a basic bug was in this thing.

@jvdwetering jvdwetering added the bug Something isn't working label Aug 23, 2023
@jvdwetering jvdwetering self-assigned this Aug 23, 2023
bichselb pushed a commit to bichselb/pyzx that referenced this issue Aug 24, 2023
@bichselb
Copy link
Author

Ok, thanks for the quick confirmation and detailed explanation!

As this issue is a bottleneck for me, I tried to fix it myself in the pull request above.

jvdwetering added a commit that referenced this issue Aug 24, 2023
Fix #134: incorrect optimization for qiskit circuit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants