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

QASM exporter error with CSwapGate and CCXGate: Cannot find gate definition for 'unitary...' #8222

Closed
MattePalte opened this issue Jun 22, 2022 · 1 comment · Fixed by #9953
Labels
bug Something isn't working

Comments

@MattePalte
Copy link

Environment

  • Qiskit Terra version: 0.19.1
  • Python version: 3.8
  • Operating system: Ubuntu 18.04.6 LTS

What is happening?

Using the CSwapGate and CCXGate in a subcircuit and converting it to qasm leads to an incorrect qasm file, where a unitary is never defined. Note that this happens only with optimization level 3 and not with others.

How can we reproduce the issue?

Run the following script:

import qiskit
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit.library.standard_gates import *
from qiskit import Aer, transpile, execute

qr = QuantumRegister(3, name='qr')
cr = ClassicalRegister(3, name='cr')
qc = QuantumCircuit(qr, cr, name='qc')
subcircuit = QuantumCircuit(qr, name='subcircuit')
subcircuit.append(CSwapGate(), qargs=[qr[0], qr[1], qr[2]], cargs=[])
subcircuit.append(CCXGate(), qargs=[qr[0], qr[1], qr[2]], cargs=[])
qc.append(subcircuit, qargs=qr)
qc.append(subcircuit.inverse(), qargs=qr)
qc.measure(qr, cr)
qc = transpile(qc, optimization_level=3)
my_qasm = qc.qasm()
print(my_qasm)

Output qasm:

OPENQASM 2.0;
include "qelib1.inc";
gate unitary139839044026176 p0,p1 {
	u3(pi/2,pi/2,-pi/2) p0;
	u3(pi/2,0,-1.569473) p1;
	cx p0,p1;
	u3(pi/2,-pi/2,pi/2) p0;
	u3(3.1402694,-3*pi/4,pi/2) p1;
}
qreg qr[3];
creg cr[3];
unitary139839044026176 qr[1],qr[2];
cx qr[0],qr[2];
t qr[2];
cx qr[1],qr[2];
t qr[1];
tdg qr[2];
cx qr[0],qr[2];
cx qr[0],qr[1];
t qr[0];
tdg qr[1];
cx qr[0],qr[1];
t qr[2];
h qr[2];
unitary139839044101600 qr[1],qr[2];
cx qr[0],qr[2];
t qr[2];
cx qr[1],qr[2];
t qr[1];
tdg qr[2];
cx qr[0],qr[2];
cx qr[0],qr[1];
t qr[0];
tdg qr[1];
cx qr[0],qr[1];
t qr[2];
cx qr[1],qr[2];
tdg qr[2];
cx qr[0],qr[2];
t qr[2];
cx qr[1],qr[2];
t qr[1];
tdg qr[2];
cx qr[0],qr[2];
cx qr[0],qr[1];
t qr[0];
tdg qr[1];
cx qr[0],qr[1];
t qr[2];
h qr[2];
unitary139839044129696 qr[1],qr[2];
cx qr[0],qr[2];
t qr[2];
cx qr[1],qr[2];
t qr[1];
tdg qr[2];
cx qr[0],qr[2];
cx qr[0],qr[1];
t qr[0];
tdg qr[1];
cx qr[0],qr[1];
t qr[2];
h qr[2];
cx qr[2],qr[1];
measure qr[0] -> cr[0];
measure qr[1] -> cr[1];
measure qr[2] -> cr[2];

Read the qasm:

qc = QuantumCircuit.from_qasm_str(my_qasm)

output:

QasmError: "Cannot find gate definition for 'unitary139839047598864', line 25 file "

What should happen?

The QASM exporter should create the definition for unitary139839047598864 as well.

Any suggestions?

This is similar to another issue (#7772) but in that case the problem was with CSwap and DCX, thus the error might be due to the interaction between the CSwap and other CX gates.

@MattePalte MattePalte added the bug Something isn't working label Jun 22, 2022
@jakelishman
Copy link
Member

This is likely because UnitaryGate has some pretty questionable special handling to turn itself into OQ 2, including statefully producing a custom definition (the only gate in the circuit library to do so). Probably this false definition is getting copied into an object that it should be in, or some other confusion like this, but neither the OQ 2 exporter nor UnitaryGate behave well and predictably, so it's pretty hard to know.

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