I'm just getting started with OpenQASM, but I'm feeling a bit confused. Is there a way to run the .qasm file I've written in Qiskit (i.e. in Python), or is OpenQasm only meant to be used within the IBM Quantum experience?


1 Answer 1


You can use qasm files to create a quantum circuit, two ways : via the methods from_qasm_str or from_qasm_file on a QuantumCircuit. See the documentation about this. For example :

test = ''' OPENQASM 2.0;
include "qelib1.inc";

qreg q[2];
creg c[2];

h q[0];
cx q[0],q[1];
from qiskit import QuantumCircuit
qc = QuantumCircuit.from_qasm_str(test)

This will create a QuantumCircuit from the str test. The same syntax work with the other method, just replace test with the path to the qasm file.

Hope this helps! :)

  • $\begingroup$ Thank you so much! That appears to have worked. I do have another question though. How would I go about getting the state from this QuantumCircuit object? I've tried save_state() but that throws an error. Do I have to then run this on a simulator (such as aer) to be able to retrieve the state? Maybe this ought to be a separate post $\endgroup$
    – stare100
    Commented Aug 18, 2021 at 1:49
  • $\begingroup$ You can find the answer in this tutorial :) $\endgroup$
    – Lena
    Commented Aug 18, 2021 at 7:39

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.