tequilahub / tequila

A High-Level Abstraction Framework for Quantum Algorithms
MIT License
369 stars 103 forks source link

Z gate .dagger failed #333

Closed andrew-koren closed 7 months ago

andrew-koren commented 8 months ago

Describe the bug After importing openqasm circuit to tequila using tq.import_open_qasm(), the hermitian conjugate of the circuit containing the gate Z(target=(1,), control=(0,)) could not be found. The hermeitian conjugate of this gate could not be calculated using .dagger(). In addition, this gate has no generator.

To Reproduce

import tequila as tq

imported_circuit = tq.import_open_qasm('// Generated from Cirq v1.3.0\n\nOPENQASM 2.0;\ninclude "qelib1.inc";\n\n\n// Qubits: [q(0), q(1), q(2), q(3)]\nqreg q[4];\n\n\nu3(pi*0.5,pi*1.25,pi*0.75) q[0];\nu3(pi*0.25,pi*1.5,pi*0.5) q[1];\ncz q[0],q[1];\nu3(pi*1.5,pi*1.25,pi*0.75) q[0];\nu3(pi*0.5,0,0) q[1];\ncz q[0],q[1];\nu3(pi*0.25,pi*0.5,pi*0.25) q[0];\nu3(pi*0.5,pi*1.25,pi*1.0) q[1];\nu3(pi*0.5,pi*1.25,pi*0.75) q[2];\nu3(pi*0.25,pi*1.5,pi*0.5) q[3];\ncz q[2],q[3];\nu3(pi*1.5,pi*1.25,pi*0.75) q[2];\nu3(pi*0.5,0,0) q[3];\ncz q[2],q[3];\nu3(pi*0.25,pi*0.5,pi*0.25) q[2];\nu3(pi*0.5,pi*1.25,pi*1.0) q[3];\n')
#imported_circuit.dagger()

for gate in imported_circuit.gates:
    print(gate)
    print(gate.generator)
    print(gate.dagger())

Expected behavior Imported circuit hermetian conjugate should be returned

Computer (please complete the following information): tequila version: 1.9.4 python version: 3.10.13 (main, Feb 6 2024, 19:53:26) [GCC 9.4.0] platform: https://github.com/tequilahub/tequila/issues/19~22.04.1-Ubuntu SMP Wed Jan 10 22:57:03 UTC 2024

kottmanj commented 8 months ago

Thanks for noticing.

I think I know the underlying issue, it's basically because the open-qasm import module uses calls to low-level functions to initialize gates: Example:

gates.impl.QGateImpl(....)

instead of

gates.Z(....)

In principle all gates.impl.XYimpl(name=NAME,....) calls in the function parse_command in the file src/tequila/circuit/qasm.py need to be replaced by the corresponding gates.NAME(...) call.

All initialization routines in tq.gates will set generators correctly. (generators for all gates where introduced after the open-qasm module)

In principle not hard to fix, but will need a bit of time due to other things.

kottmanj commented 8 months ago

Feel free to do a PR if you are interested in fixing it yourself :-) Otherwise I try to get it done by the end of march

kottmanj commented 7 months ago

Should work now. Please let me know if not. Tanks for reporting!

andrew-koren commented 7 months ago

Success! I was able to verify this works, along with import S gate