pytket.qasm

Circuit objects can be converted to and from OpenQASM, although we do not support all operations.

However, we do support symbolic parameters of gates, both on import and export.

Any pytket Circuit that is exported to OpenQASM format with pytket.qasm should be valid for importing again as a Circuit, making this a convenient file format to save your Circuit objects.

In addition to the default qelib1 qasm header, the hqslib1 header is also supported. We can set the header argument in the qasm conversion functions as follows.

from pytket.qasm import circuit_to_qasm_str

qasm_str = circuit_to_qasm_str(circ, header="hqslib1")

Note

Unlike pytket backends, the qasm converters do not handle implicit qubit permutations. In other words if a circuit containing an implicit qubit permutation is converted to a qasm file the implicit permutation will not be accounted for and the circuit will be missing this permutation when reimported.

Parser from OPENQASM to tket Circuits

pytket.qasm.circuit_from_qasm(input_file, encoding='utf-8', maxwidth=32, use_clexpr=False)[source]

A method to generate a tket Circuit from a qasm file.

Parameters:
  • input_file (Union[str, PathLike[Any]]) – path to qasm file; filename must have .qasm extension

  • encoding (str) – file encoding (default utf-8)

  • maxwidth (int) – maximum allowed width of classical registers (default 32)

  • use_clexpr (bool) – whether to use ClExprOp to represent classical expressions

Return type:

Circuit

Returns:

pytket circuit

pytket.qasm.circuit_from_qasm_io(stream_in, maxwidth=32, use_clexpr=False)[source]

A method to generate a tket Circuit from a qasm text stream

Return type:

Circuit

pytket.qasm.circuit_from_qasm_str(qasm_str, maxwidth=32, use_clexpr=False)[source]

A method to generate a tket Circuit from a qasm string.

Parameters:
  • qasm_str (str) – qasm string

  • maxwidth (int) – maximum allowed width of classical registers (default 32)

  • use_clexpr (bool) – whether to use ClExprOp to represent classical expressions

Return type:

Circuit

Returns:

pytket circuit

pytket.qasm.circuit_from_qasm_wasm(input_file, wasm_file, encoding='utf-8', maxwidth=32, use_clexpr=False)[source]

A method to generate a tket Circuit from a qasm string and external WASM module.

Parameters:
  • input_file (Union[str, PathLike[Any]]) – path to qasm file; filename must have .qasm extension

  • wasm_file (Union[str, PathLike[Any]]) – path to WASM file containing functions used in qasm

  • encoding (str) – encoding of qasm file (default utf-8)

  • maxwidth (int) – maximum allowed width of classical registers (default 32)

Return type:

Circuit

Returns:

pytket circuit

pytket.qasm.circuit_to_qasm(circ, output_file, header='qelib1', maxwidth=32)[source]

Convert a Circuit to QASM and write it to a file.

Classical bits in the pytket circuit must be singly-indexed.

Note that this will not account for implicit qubit permutations in the Circuit.

Parameters:
  • circ (Circuit) – pytket circuit

  • output_file (str) – path to output qasm file

  • header (str) – qasm header (default “qelib1”)

  • maxwidth (int) – maximum allowed width of classical registers (default 32)

Return type:

None

pytket.qasm.circuit_to_qasm_io(circ, stream_out, header='qelib1', include_gate_defs=None, maxwidth=32)[source]

Convert a Circuit to QASM and write to a text stream.

Classical bits in the pytket circuit must be singly-indexed.

Note that this will not account for implicit qubit permutations in the Circuit.

Parameters:
  • circ (Circuit) – pytket circuit

  • stream_out (TextIO) – text stream to be written to

  • header (str) – qasm header (default “qelib1”)

  • include_gate_defs (Optional[Set[str]]) – optional set of gates to include

  • maxwidth (int) – maximum allowed width of classical registers (default 32)

Return type:

None

pytket.qasm.circuit_to_qasm_str(circ, header='qelib1', include_gate_defs=None, maxwidth=32)[source]

Convert a Circuit to QASM and return the string.

Classical bits in the pytket circuit must be singly-indexed.

Note that this will not account for implicit qubit permutations in the Circuit.

Parameters:
  • circ (Circuit) – pytket circuit

  • header (str) – qasm header (default “qelib1”)

  • output_file – path to output qasm file

  • include_gate_defs (Optional[Set[str]]) – optional set of gates to include

  • maxwidth (int) – maximum allowed width of classical registers (default 32)

Return type:

str

Returns:

qasm string