mimiqcircuits.qasm.interpreter

Functions

compile_pending_gate(istate, name)

create_c3sqrtx(*args)

create_c4x(*args)

create_cu1(*args)

create_cu3(*args)

interpret(expr)

interpret_barrier(istate, expr)

interpret_expression(istate, expr)

interpret_gatecall(istate, expr)

interpret_gatedecl(istate, expr)

interpret_gop(istate, expr)

interpret_if(istate, expr)

interpret_measure(istate, expr)

interpret_opaque(istate, expr)

interpret_qop(istate, expr)

interpret_regs(istate, expr)

interpret_reset(istate, expr)

interpret_target(istate, expr)

interpret_targets(istate, expr)

interpret_unitary(istate, expr)

load(filename[, includedirs])

Read an OpenQASM 2.0 program from a file.

loads(s[, includedirs])

Read an OpenQASM 2.0 program from a string.

token_val_to_str(val)

Classes

InterpreterState(regs, range] =, lastqreg, ...)

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. include files are looked up in the current directory, then in the directory of files bundled with the package (which provides qelib1.inc), then in includedirs.

Parameters:
  • s (str) – The OpenQASM 2.0 source.

  • includedirs (List[str]) – Extra directories to search for included files. Defaults to None.

Returns:

The circuit described by the program.

Return type:

Circuit

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 of includedirs.

Parameters:
  • filename (str) – The path of the file.

  • includedirs (List[str]) – Extra directories to search for included files. Defaults to None.

Returns:

The circuit described by the program.

Return type:

Circuit

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]