mimiqcircuits.qasm.interpreter¶
Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Read an OpenQASM 2.0 program from a file. |
|
Read an OpenQASM 2.0 program from a string. |
|
Classes
|
- class InterpreterState(regs: Dict[str, range] = <factory>, lastqreg: int = 0, lastcreg: int = 0, gates: Dict[str, Any] = <factory>, pending_gates: Dict[str, Any] = <factory>, compiling_gates: Set[str] = <factory>, vars_map: Dict[str, Any] = <factory>, circuit: mimiqcircuits.circuit.Circuit = <factory>)[source]¶
Bases:
object
- loads(s, includedirs=None)[source]¶
Read an OpenQASM 2.0 program from a string.
Quantum registers are laid out one after the other on the circuit’s qubits in order of declaration, and classical registers likewise on its bits.
includefiles are looked up in the current directory, then in the directory of files bundled with the package (which providesqelib1.inc), then inincludedirs.- Parameters:
- Returns:
The circuit described by the program.
- Return type:
- Raises:
ParseError – If the source is not valid OpenQASM 2.0 syntax or an included file is not found.
QASMError – If the program cannot be interpreted, for example because it uses an undeclared gate or register (see the subclasses in
mimiqcircuits.qasm).
Examples
>>> from mimiqcircuits import * >>> from mimiqcircuits import qasm
A Bell-state program using gates from
qelib1.inc.>>> src = ''' ... OPENQASM 2.0; ... include "qelib1.inc"; ... qreg q[2]; ... creg c[2]; ... h q[0]; ... cx q[0],q[1]; ... measure q -> c; ... ''' >>> qasm.loads(src) 2-qubit, 2-bit circuit with 4 instructions: ├── H @ q[0] ├── CX @ q[0], q[1] ├── M @ q[0], c[0] └── M @ q[1], c[1]
- load(filename, includedirs=None)[source]¶
Read an OpenQASM 2.0 program from a file.
Same as
loads()on the file’s content, with the file’s own directory added to the front ofincludedirs.- Parameters:
- Returns:
The circuit described by the program.
- Return type:
- Raises:
FileNotFoundError – If the file does not exist.
ParseError – If the source is not valid OpenQASM 2.0 syntax or an included file is not found.
QASMError – If the program cannot be interpreted.
Examples
Write a circuit to a file with
dump()and read it back.>>> from mimiqcircuits import * >>> from mimiqcircuits import qasm >>> import os, tempfile >>> c = Circuit() >>> c.push(GateH(), 0) 1-qubit circuit with 1 instruction: └── H @ q[0] >>> path = os.path.join(tempfile.mkdtemp(), "circuit.qasm") >>> qasm.dump(c, path) >>> qasm.load(path) 1-qubit circuit with 1 instruction: └── H @ q[0]