OpenQASM module¶
- class OpenQASM¶
Implements the OpenQASM class. Use this class to import OpenQASM 3.0 compliant files to build a quantum circuit.
Circuits that contain control flow referencing mid-circuit measured classical bits, or that use
input/outputdirectives, cannot be imported using this class. UseBackendV2.runinstead.
- OpenQASM(self)¶
The default constructor.
- Example:
>>> oq = OpenQASM()- Args:
None
- Returns:
OpenQASM object
- Raises:
None.
- load(self, filename, global_phase=0)¶
Constructs a quantum circuit out of the instructions contained in the provided OpenQASM 3.0 file.
- Example:
>>> import math >>> oq = OpenQASM() >>> qc = oq.load("my_circuit.qasm")
With an optional global phase: >>> qc = oq.load(“my_circuit.qasm”, math.pi / 8)
- Args:
- filename (str):
The name of the source file containing the OpenQASM 3.0 code.
- global_phase (Optional(float)):
Default: 0 A global phase to apply to the circuit, in radians.
- Returns:
The handle to the quantum circuit.
- Raises:
RuntimeError, if the input file has an error. Error E83, if the circuit contains control flow that references mid-circuit measured classical bits. Use
BackendV2.runinstead.
- loads(self, qasm3string, global_phase=0)¶
Constructs a quantum circuit out of the instructions contained in the provided OpenQASM 3.0 string.
- Example:
>>> oq = OpenQASM() >>> qasm_str = open("my_circuit.qasm", "r", encoding="utf-8").read() >>> qc = oq.loads(qasm_str)
- Args:
- qasm3string (str):
Python string containing the OpenQASM 3.0 code.
- global_phase (Optional(float)):
Default: 0 A global phase to apply to the circuit, in radians.
- Returns:
The handle to the quantum circuit.
- Raises:
RuntimeError, if the input string has an error. Error E83, if the circuit contains control flow that references mid-circuit measured classical bits. Use
BackendV2.runinstead.