Does anyone know of a library to convert a parametrized qiskit circuit drawn as 'text' back to a QuantumCircuit or similar?

I have see [this] 1 question, but unfortunately I cannot determine in which format the circuit has been saved. I need to start of a file in which the drawn circuit has been saved as text like this:

              ┌────┐    ┌────────┐ ┌──────────┐┌───┐ ░ ┌─┐
   q_0 -> 1 ──┤ √X ├────┤ Rz(3π) ├─┤ Rz(p[2]) ├┤ X ├─░─┤M├───────── 
            ┌─┴────┴─┐ ┌┴────────┴┐└──────────┘└─┬─┘ ░ └╥┘   ┌─┐    

 meas: 4/════════════════════════════════════════════╩══╩══╩══╩═ 

In more detail the saved file in my case is a text file looking like this: It is a dictionary converted to a string using json. The values of the dictionary are lists of the ascii-art strings of the circuits generated by QuantumCircuit.draw():

{'ansatz 1':"['           ┌───┐     ┌──────────┐ ┌──────────┐┌──────────┐┌───┐      ░ ┌─┐   »\n   q_0: ───┤ X ├─────┤ Rz(p[0]) ├─┤ Ry(p[1]) ├┤ Rz(p[2]) ├┤ X ├──────░─┤M├───»\n           ├───┤     ├──────────┤ ├──────────┤├──────────┤└─┬─┘┌───┐ ░ └╥┘┌─┐»\n   q_1: ───┤ X ├─────┤ Rz(p[3]) ├─┤ Ry(p[4]) ├┤ Rz(p[5]) ├──┼──┤ X ├─░──╫─┤M├»\n        ┌──┴───┴───┐ ├──────────┤ ├──────────┤└──────────┘  │  └─┬─┘ ░  ║ └╥┘»\n   q_2: ┤ Rz(p[6]) ├─┤ Ry(p[7]) ├─┤ Rz(p[8]) ├─────■────────■────┼───░──╫──╫─»\n        ├──────────┤┌┴──────────┤┌┴──────────┤   ┌─┴─┐           │   ░  ║  ║ »\n   q_3: ┤ Rz(p[9]) ├┤ Ry(p[10]) ├┤ Rz(p[11]) ├───┤ X ├───────────■───░──╫──╫─»\n        └──────────┘└───────────┘└───────────┘   └───┘               ░  ║  ║ »\nmeas: 4/════════════════════════════════════════════════════════════════╩══╩═»\n                                                                        0  1 »\n«              \n«   q_0: ──────\n«              \n«   q_1: ──────\n«        ┌─┐   \n«   q_2: ┤M├───\n«        └╥┘┌─┐\n«   q_3: ─╫─┤M├\n«         ║ └╥┘\n«meas: 4/═╩══╩═\n«         2  3 ', '...}
  • 2
    $\begingroup$ Out of curiousity, why are you looking for a tool to do this? $\endgroup$
    – forky40
    Dec 29, 2022 at 23:09
  • $\begingroup$ Can ChatGPT give the result? $\endgroup$
    – narip
    Dec 30, 2022 at 7:11
  • $\begingroup$ @forky40 I want to do a comparison of runs of an algorithm done on real quantum hardware with runs with a custom noise model in simulations. Therefore, I want to use the exact same circuits in the simulations as the ones used for the runs on hardware that were saved in this format. $\endgroup$ Dec 30, 2022 at 10:03
  • 1
    $\begingroup$ @qcabepsilon If storing the circuit to disk and import again is what you are trying to do, you can convert the circuit to QASM strings using QuantumCircuit.qasm() method and load it using QuantumCircuit.from_qasm_str() method or QuantumCircuit.from_qasm_file() method. I can write a detailed answer if this is what you want. $\endgroup$ Jan 2, 2023 at 16:51
  • 1
    $\begingroup$ @qcabepsilon maybe this can help you understand how to use qasm(): qiskit.org/documentation/stubs/… , from_qasm_str(): qiskit.org/documentation/stubs/… $\endgroup$
    – poig
    Jan 5, 2023 at 4:24

2 Answers 2


You can try ChatGpt, this is what I got according to your text:
Note that, it doesn't work accurately every time, you can try to specify more in detail, or point out the error.
I said:

              ┌────┐    ┌────────┐ ┌──────────┐┌───┐ ░ ┌─┐
   q_0 -> 1 ──┤ √X ├────┤ Rz(3π) ├─┤ Rz(p[2]) ├┤ X ├─░─┤M├───────── 
            ┌─┴────┴─┐ ┌┴────────┴┐└──────────┘└─┬─┘ ░ └╥┘   ┌─┐    

 meas: 4/════════════════════════════════════════════╩══╩══╩══╩═
can you convert it into qiskit quantum circuit code

ChatGPT reply:

from qiskit import QuantumCircuit, QuantumRegister, execute, Aer

# Create a quantum circuit with a single qubit and a single classical bit
qreg = QuantumRegister(1)
creg = ClassicalRegister(1)
qc = QuantumCircuit(qreg, creg)

# Apply the gates in the circuit
qc.u3(3*pi, 0, 0, qreg[0])  # √X gate
qc.rz(3*pi, qreg[0])  # Rz(3π) gate
qc.rz(p[2], qreg[0])  # Rz(p[2]) gate
qc.x(qreg[0])  # X gate

# Measure the qubit
qc.measure(qreg, creg)

# Execute the circuit using a simulator
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1)

# Print the measurement result

  • $\begingroup$ That is nice and quite impressive. But I'm rather looking for a general code, where I can give the string as input and get the circuit as a return, instead of asking ChatGPT for each single circuit. $\endgroup$ Jan 2, 2023 at 10:06
  • 1
    $\begingroup$ ahh, if you are asking for saving the circuit as a string, just use qasm, other then qasm, there is also quil. But since you are using Qiskit, and qasm is more popular around different frameworks, you can just do what @JunyeHuang said. $\endgroup$
    – poig
    Jan 2, 2023 at 23:44

As explained in the comments, you can export QuantumCircuit object as qasm string or file and reload it later into QuantumCircuit again using the QuantumCircuit.from_qasm_string and Quantumcircuit.from_qasm_file methods.

from qiskit import QuantumCircuit
qc = QuantumCircuit(1)

# export qasm string to a variable
qasm_string = qc.qasm()
qc_from_string = QuantumCircuit.from_qasm_str(qasm_string)
print(qc == qc_from_string)
# Output: True

# store qasm string into a file
with open('qasm_file', 'w') as output_file:

qc_from_file = QuantumCircuit.from_qasm_file('qasm_file')
print(qc == qc_from_file)
# Output: True

You can find the jupyter notebook with full code and output here: https://gist.github.com/HuangJunye/02bdcd927de4aab0cfc214428fa1fdac

Do note that there are limitations of converting QuantumCircuit as qasm as OpenQASM2 does not support all features such as unbound parameters. Exporting to Qobj or qpy as explained in this answer is better because the conversion is lossless.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.