pytket.program

class pytket.program.Program

Encapsulates a control flow graph for a quantum program. Each basic block is a single quantum circuit which may include classical instructions and OpenQASM-style conditional gates. Branches are always made using a single condition bit. Allows long sequences of operations to be applied conditionally or repeatedly while some bit is true.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pytket.program.Program) -> None

Constructs an empty program.

  1. __init__(self: pytket.program.Program, n_qubits: int, n_bits: int = 0) -> None

Constructs a program with a given number of quantum and classical bits

Parameters
  • n_qubits – The number of qubits in the program

  • c_bits – The number of classical bits in the program

add_bit(self: pytket.program.Program, id: pytket.circuit.Bit, reject_dups: bool = True) None

Constructs a single bit with the given id.

Parameters
  • id – Unique id for the bit

  • reject_dups – Fail if there is already a bit in this program with the id. Default to True

add_c_register(self: pytket.program.Program, name: str, size: int) Dict[int, pytket.circuit.UnitID]

Constructs a new classical register with a given name and number of bits.

Parameters
  • name – Unique readable name for the register

  • size – Number of bits required

Returns

a map from index to the corresponding UnitIDs

add_gate(*args, **kwargs)

Overloaded function.

  1. add_gate(self: pytket.program.Program, type: pytket.circuit.OpType, args: List[int], **kwargs) -> pytket.program.Program

Appends a single (non-parameterised) gate to the end of the program on some particular qubits from the default register (‘q’). The number of qubits specified must match the arity of the gate.

Parameters
  • type – The type of operation to add

  • args – The list of indices for the qubits/bits to which the operation is applied

  • kwargs – Additional properties for classical conditions

Returns

the new Program

  1. add_gate(self: pytket.program.Program, type: pytket.circuit.OpType, angle: Expression, args: List[int], **kwargs) -> pytket.program.Program

Appends a single gate, parameterised by an expression, to the end of the program on some particular qubits from the default register (‘q’).

Parameters
  • type – The type of gate to add

  • angle – The parameter for the gate in halfturns

  • args – The list of indices for the qubits/bits to which the operation is applied

  • kwargs – Additional properties for classical conditions

Returns

the new Program

  1. add_gate(self: pytket.program.Program, type: pytket.circuit.OpType, angles: List[Expression], args: List[int], **kwargs) -> pytket.program.Program

Appends a single gate, parameterised with a vector of expressions corresponding to halfturns, to the end of the program on some particular qubits from the default register (‘q’).

Parameters
  • type – The type of gate to add

  • angles – The parameters for the gate in halfturns

  • args – The list of indices for the qubits/bits to which the operation is applied

  • kwargs – Additional properties for classical conditions

Returns

the new Program

  1. add_gate(self: pytket.program.Program, type: pytket.circuit.OpType, params: List[Expression], args: List[pytket.circuit.UnitID], **kwargs) -> pytket.program.Program

Appends a single gate to the end of the program

Parameters
  • type – The type of gate to add

  • params – The parameters for the gate in halfturns

  • args – The qubits/bits to apply the gate to

  • kwargs – Additional properties for classical conditions

Returns

the new Program

add_q_register(self: pytket.program.Program, name: str, size: int) Dict[int, pytket.circuit.UnitID]

Constructs a new quantum register with a given name and number of qubits.

Parameters
  • name – Unique readable name for the register

  • size – Number of qubits required

Returns

a map from index to the corresponding UnitIDs

add_qubit(self: pytket.program.Program, id: pytket.circuit.Qubit, reject_dups: bool = True) None

Constructs a single qubit with the given id.

Parameters
  • id – Unique id for the qubit

  • reject_dups – Fail if there is already a qubit in this program with the id. Default to True

append(self: pytket.program.Program, prog: pytket.program.Program) None

In-place sequential composition of programs, appending a copy of the argument onto the end of self.

Parameters

prog – The program to be appended to the end of self

append_circuit(self: pytket.program.Program, circuit: pytket.circuit.Circuit) pytket.program.Program

Appends a circuit to the end of the program

Parameters

circuit – The circuit to add

Returns

the new Program

append_if(self: pytket.program.Program, condition_bit: pytket.circuit.Bit, body: pytket.program.Program) None

In-place sequential composition of programs, performing body after self if the condition_bit is found to be 1.

Parameters
  • condition_bit – A single bit condition.

  • body – The program to be applied after self if condition_bit is 1.

append_if_else(self: pytket.program.Program, condition_bit: pytket.circuit.Bit, if_body: pytket.program.Program, else_body: pytket.program.Program) None
Parameters

else_body – The program to be applied after self if condition_bit is 0.

append_while(self: pytket.program.Program, condition_bit: pytket.circuit.Bit, body: pytket.program.Program) None

In-place sequential composition of programs, performing body after self repeatedly whilst the condition_bit is found to be 1.

Parameters
  • condition_bit – A single bit condition.

  • body – The program to be applied after self repeatedly whilst condition_bit is 1.

get_commands(self: pytket.program.Program) List[pytket.circuit.Command]
Returns

a list of all the Commands in the program

property bit_readout

A map from bit to its (left-to-right) index in readouts from backends (following the increasing lexicographic order convention)

property bits

A list of all classical bit ids in the program

property qubit_readout

A map from qubit to its (left-to-right) index in readouts from backends

property qubits

A list of all qubit ids in the program