mimiqcircuits.qasm.serializer

Functions

args_to_qasm(gate)

circuit_to_qasm(c[, sanitize_names])

collect_gatedecls(c[, sanitize_names])

dump(c, filename[, decompose_wrappers, ...])

Write a circuit as an OpenQASM 2.0 program to a file.

dumps(c[, decompose_wrappers, sanitize_names])

Write a circuit as an OpenQASM 2.0 program.

gate_to_qasm(gate)

gatedecl_to_qasm(decl, decl_names[, ...])

instruction_to_qasm(inst, num_qubits[, ...])

print_qasm_expr(expr, io_stream[, level])

qubits_to_qasm(qubits, size)

uses_std_gates(instructions)

dumps(c, decompose_wrappers=False, sanitize_names=True)[source]

Write a circuit as an OpenQASM 2.0 program.

Operations that OpenQASM 2.0 cannot express directly are decomposed into qelib1.inc gates, and gate declarations (GateDecl) become gate definitions. All qubits go in one register q and all bits in one register c.

Parameters:
  • c (Circuit) – The circuit to write.

  • decompose_wrappers (bool) – Not used.

  • sanitize_names (bool) – If True, rewrite gate declaration names into valid OpenQASM identifiers. If False, an invalid name raises QASMSerializationError. Defaults to True.

Returns:

The OpenQASM 2.0 source.

Return type:

str

Raises:
  • DecompositionError – If an operation cannot be decomposed into the OpenQASM 2.0 basis, such as a noise channel or a classical or z-variable operation.

  • QASMSerializationError – If a name or an IfStatement condition cannot be expressed in OpenQASM 2.0.

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits import qasm
>>> c = Circuit()
>>> c.push(GateH(), 0)
1-qubit circuit with 1 instruction:
└── H @ q[0]

>>> c.push(GateCX(), 0, 1)
2-qubit circuit with 2 instructions:
├── H @ q[0]
└── CX @ q[0], q[1]

>>> c.push(Measure(), range(2), range(2))
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]

>>> print(qasm.dumps(c))
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0],q[1];
measure q[0] -> c[0];
measure q[1] -> c[1];
dump(c, filename, decompose_wrappers=False, sanitize_names=True)[source]

Write a circuit as an OpenQASM 2.0 program to a file.

The file is overwritten, unless serialization fails. See dumps() for how the circuit is written; load() reads the file back.

Parameters:
  • c (Circuit) – The circuit to write.

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

  • decompose_wrappers (bool) – Not used.

  • sanitize_names (bool) – If True, rewrite gate declaration names into valid OpenQASM identifiers. Defaults to True.

Raises: