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: str | PathLike[Any], encoding: str = 'utf-8', maxwidth: int = 32) Circuit [source]¶
A method to generate a tket Circuit from a qasm file.
- Parameters:
input_file – path to qasm file; filename must have
.qasm
extensionencoding – file encoding (default utf-8)
maxwidth – maximum allowed width of classical registers (default 32)
- Returns:
pytket circuit
- pytket.qasm.circuit_from_qasm_io(stream_in: TextIO, maxwidth: int = 32) Circuit [source]¶
A method to generate a tket Circuit from a qasm text stream
- pytket.qasm.circuit_from_qasm_str(qasm_str: str, maxwidth: int = 32) Circuit [source]¶
A method to generate a tket Circuit from a qasm string.
- Parameters:
qasm_str – qasm string
maxwidth – maximum allowed width of classical registers (default 32)
- Returns:
pytket circuit
- pytket.qasm.circuit_from_qasm_wasm(input_file: str | PathLike[Any], wasm_file: str | PathLike[Any], encoding: str = 'utf-8', maxwidth: int = 32) Circuit [source]¶
A method to generate a tket Circuit from a qasm string and external WASM module.
- Parameters:
input_file – path to qasm file; filename must have
.qasm
extensionwasm_file – path to WASM file containing functions used in qasm
encoding – encoding of qasm file (default utf-8)
maxwidth – maximum allowed width of classical registers (default 32)
- Returns:
pytket circuit
- pytket.qasm.circuit_to_qasm(circ: Circuit, output_file: str, header: str = 'qelib1', maxwidth: int = 32) None [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 – pytket circuit
output_file – path to output qasm file
header – qasm header (default “qelib1”)
maxwidth – maximum allowed width of classical registers (default 32)
- pytket.qasm.circuit_to_qasm_io(circ: Circuit, stream_out: TextIO, header: str = 'qelib1', include_gate_defs: Set[str] | None = None, maxwidth: int = 32) None [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 – pytket circuit
stream_out – text stream to be written to
header – qasm header (default “qelib1”)
include_gate_defs – optional set of gates to include
maxwidth – maximum allowed width of classical registers (default 32)
- pytket.qasm.circuit_to_qasm_str(circ: Circuit, header: str = 'qelib1', include_gate_defs: Set[str] | None = None, maxwidth: int = 32) str [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 – pytket circuit
header – qasm header (default “qelib1”)
output_file – path to output qasm file
include_gate_defs – optional set of gates to include
maxwidth – maximum allowed width of classical registers (default 32)
- Returns:
qasm string