# Conversion error: from a QISKIT circuit to a QASM string and back

I created a circuit in qiskit and then converted it into a QASM string. When I try to make a circuit out of the QASM string I get the error:

QasmError: "Cannot find gate definition for 'mcphase', line 3 file circuit.qasm"


I used the following code to convert a circuit into a QASM file:

qasm_file_name = "circuit.qasm"
main_circuit.qasm(formatted=True, filename=qasm_file_name)



Then I loaded the QASM string like so:

circuit = QuantumCircuit.from_qasm_file(qasm_file_name)


After this, I get the QASM error above.

I believe mcphase gate was generated automatically during the conversion to QASM. It seems the mapping from a qiskit circuit to QASM string is not reversible. How this can be fixed?

This is a toy QASM file that produces the error.

OPENQASM 2.0;
include "qelib1.inc";
gate ccircuit_87_dg q0,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20 { cx q0,q4; cu(pi/2,0,pi,0) q0,q4; mcphase(pi/8) q0,q4; cu(pi/2,0,pi,0) q0,q6;}


This is a full qiskit version printout:

{'qiskit-terra': '0.22.2', 'qiskit-aer': '0.11.1', 'qiskit-ignis': '0.7.0', 'qiskit-ibmq-provider': '0.19.2', 'qiskit': '0.39.2', 'qiskit-nature': None, 'qiskit-finance': None, 'qiskit-optimization': None, 'qiskit-machine-learning': None}


Ubuntu 20.04.5 LTS

• It works with me using the latest Qiskit version. What version do you have? Dec 8, 2022 at 21:05
• It would help to know what your main_circuit is. Without that, it's hard to reproduce your error. Did you transpile or assemble your circuit before writing? Dec 8, 2022 at 21:45
• @Egretta.Thula I have included the Qiskit version into the post. Dec 8, 2022 at 23:21
• @FrankYellin I can't post the main_circuit nor I can easily distill it to a toy problem, at least for now. Dec 8, 2022 at 23:25
• Is this is Qiskit version or Terra version? You can get it using: qiskit.__qiskit_version__. Also, if you can't post the circuit, could you add the first few lines of circuit.qasm file? Dec 9, 2022 at 7:25

It is a bug. And it has been reported several times before. Here are some workarounds to overcome this issue. Try the one you prefer:

1. Add the sub-circuits using compose instead of append
main_circuit.compose(sub_circuit, qubits = [...], inplace = True)

1. Transpile the circuit into elementary gates
main_circuit = transpile(main_circuit, basis_gates = ['u', 'cx'])


If you prefer to keep the structure of the saved circuit as close as possible to the original circuit, you can decompose the gates that cause the issue only:

sub_circuit.append(MCPhaseGate(pi / 8, num_ctrl_qubits = 1, label = 'mcp'), [1, 2])
sub_circuit = sub_circuit.decompose(gates_to_decompose=['mcp'])

1. Add the missing definition(s) to Qasm:
qasm_file_name = 'circuit.qasm'

qasm_file_in = open(qasm_file_name, 'r')
qasm_file_in.close()

qasm_str = qasm_str.replace('include "qelib1.inc";', 'include "qelib1.inc";\ngate mcphase(param0) q0,q1 { cp(param0) q0,q1; }')
circuit = QuantumCircuit.from_qasm_str(qasm_str)


I think I've figured out the problem!

import math
from qiskit import *
from qiskit.circuit.library.standard_gates import *

qc = QuantumCircuit(3)
qc.append(PhaseGate(math.pi/5).control(2), (0, 1, 2))
output = qc.qasm(formatted=True)
output2 = qc.qasm(formatted=False)
print(output)
print(output2)


You will see that the first output is the qasm code with all sort of formatting codes inserted so that it will look pretty on the terminal. The second is just your qasm code.

You are generated formatted output, and then writing it to a file.

What you should be doing is:

main_circuit.qasm(file=qasm_file_name)


which writes the qasm to the specified file and ignores the formatted= argument.

If you look at the file you've created, you should see the line

gate mcphase(param0) q0,q1,q2 { ... }


where it defines the gate you are using.

• Thank you very much. I have tried main_circuit.qasm(file=qasm_file_name) and still the same error. I have attached the QASM file that was outputted with main_circuit.qasm(file=qasm_file_name). Trying to load it will produce the aforementioned error. Dec 10, 2022 at 0:21
• You've got qasm that uses mcphase without defining it. I don't know how or why the qasm you're generating has forgotten to include the definition of mcphase. There is supposed to be a line that starts gate mcphase(param0) Dec 10, 2022 at 1:15