QASM import

Overview

QuantumRingsLib can compose a QuantumCircuit from OpenQASM 2.0 (QASM 2) content.

There are two supported entry points:

  • QuantumCircuit.from_qasm_file(path) — compose a circuit from an OpenQASM 2.0 file.

  • QuantumCircuit.from_qasm_str(qasm_str) — compose a circuit from an OpenQASM 2.0 string.

For the recommended end-to-end workflow (provider → backend → circuit → run), see Circuits.

Import from a file

Use QuantumCircuit.from_qasm_file(...) to compose a circuit from an OpenQASM 2.0 file:

from QuantumRingsLib import QuantumCircuit

qc = QuantumCircuit.from_qasm_file(r"C:\path\to\circuit.qasm")

Import from a string

Use QuantumCircuit.from_qasm_str(...) to compose a circuit from an OpenQASM 2.0 string:

from QuantumRingsLib import QuantumCircuit

# qasm_str should contain OpenQASM 2.0 instructions
qasm_str = open("circuit.qasm", "r", encoding="utf-8").read()
qc = QuantumCircuit.from_qasm_str(qasm_str)

Using the qasm2 helper

QuantumRingsLib also provides a qasm2 helper class for importing qasm2-compliant files/strings into a quantum circuit.

from QuantumRingsLib import qasm2

qa = qasm2()
qc = qa.load(r"C:\path\to\circuit.qasm")

The qasm2.load(...) and qasm2.loads(...) methods accept additional parameters (for example, an include path and a strict flag). See the API reference for details.

After importing

After the circuit is composed, you can further modify it before execution. For example, the legacy Sycamore workflow imports a circuit and then adds measurement:

qc.measure_all()

For execution, see Circuits and Run Settings.

Exporting OpenQASM 2.0

You can generate equivalent OpenQASM 2.0 code from a QuantumCircuit using qc.qasm(...). This method can print formatted output and/or save it to a file.

Common errors

  • RuntimeError can be raised if there is a problem with the instructions contained in the OpenQASM 2.0 file or string.

See also

Return to Quantum Rings SDK Documentation.