Cycle¶

class
trueq.
Cycle
(content=None, immutable=None)¶ Represents a “clock cycle” of a
Circuit
; a set ofGate
s that happen in parallel to a disjoint set of systems. The storage format is adict
that mapstuple
s of system labels toGate
objects. Any given system can only be addressed once in aCycle
.This class is iterable, yielding tuples of the form
(labels, gate)
.import trueq as tq # Single system operations can be applied to multiple systems at once cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t}) for labels, operation in cycle: print(f'Labels: {str(labels): <8}\tOperation: {operation}')
Labels: (0, 1) Operation: Gate.cx Labels: (2,) Operation: Gate.t Labels: (3,) Operation: Gate.t Labels: (4,) Operation: Gate.t
Cycles can also be labeled as
immutable
. If a cycle is set to be immutable it is then hashable, which among other properties of hashable objects it means that it may be used as a key in a dictionary. Once a cycle is set to be immutable is cannot be unset. By default if there is an operation in the cycle which acts on more than one system then the cycle is set to be immutable by default. This may be overwritten by manually settings immutable to beFalse
at the time of instantiation.import trueq as tq # By default a cycle containing 2 system operations will be immutable tq.Cycle({(0, 1): tq.Gate.cx, 2: tq.Meas()})
imm (0, 1): Gate.cx Name:

 Gate.cx
 Aliases:

 Gate.cx
 Gate.cnot
 Likeness:

 CNOT
 Generators:

 'IX': 90.0
 'ZI': 90.0
 'ZX': 90.0
 Matrix:

(2): Meas() Name:

 Meas()
import trueq as tq # This default may be mantually overwritten cycle = tq.Cycle({(0, 1): tq.Gate.cx, 2: tq.Meas()}, immutable=False) print("Cycle immutable: ", cycle.immutable) cycle.immutable = True # Trying to set immutable to False after this will raise a RuntimeError print("Cycle immutable after setting: ", cycle.immutable)
Cycle immutable: False Cycle immutable after setting: True
 Parameters
content (
dict
) – Adict
whose keys aretuple
s and whose values areOperation
s; the methodadd()
is called on each pair.immutable (
bool
) – If true, the contents of thisCycle
cannot be mutated by tools such asrandomly_compile()
.

add
(labels, operation)¶ Adds a
Operation
to thisCycle
. In particular, this can be aGate
,NativeGate
,Prep
,Block
, orMeas
.import trueq as tq cycle = tq.Cycle() cycle.add((0, 1), tq.Gate.cnot) cycle.add((2, 3, 4), tq.Gate.t) cycle
(0, 1): Gate.cx Name:

 Gate.cx
 Aliases:

 Gate.cx
 Gate.cnot
 Likeness:

 CNOT
 Generators:

 'IX': 90.0
 'ZI': 90.0
 'ZX': 90.0
 Matrix:

(2): Gate.t Name:

 Gate.t
 Aliases:

 Gate.t
 Generators:

 'Z': 45.0
 Matrix:

(3): Gate.t Name:

 Gate.t
 Aliases:

 Gate.t
 Generators:

 'Z': 45.0
 Matrix:

(4): Gate.t Name:

 Gate.t
 Aliases:

 Gate.t
 Generators:

 'Z': 45.0
 Matrix:

As a convenience, if the operation acts on one qubit (such as single qubit gates,
Meas
, orPrep
), then this operation is added to each label, even when multiple labels are provided. Parameters
labels (
int
tuple
) – Which systems this gate targets. Note that the order of labels is relevant for multisystem gates. For example, it controls the direction of a CNOT.operation (
Operation
) – The operation to add to the cycle.

property
dim
¶ The dimension of each subsystem, e.g.
2
if this cycle acts on qubits. However, if no gates have been added to this cycle, then the dimension is unknown and this value is equal toNone
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t}) cycle.dim
2
 Type
int
None

property
immutable
¶ Whether or not this cycle is immutable.
import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t}) cycle.immutable
True
 Type
bool

property
labels
¶ The sorted union of all system labels acted on by
Operation
s in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.labels
(0, 1, 2, 3, 4, 5, 6, 7)
 Type
tuple

property
n_gates
¶ The number of gates in this
Cycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.n_gates
4
 Type
int

property
n_meas
¶ The number of measurements (
Meas
) in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.n_meas
1
 Type
int

property
n_prep
¶ The number of preparations (
Prep
) in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.n_prep
1
 Type
int

property
n_block
¶ The number of preparations (
Prep
) in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.n_block
1
 Type
int

property
n_operations
¶ The total number of operations in this
Cycle
, equal to the sum ofn_gates
,n_meas
, andn_prep
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.n_operations
7
 Type
int

property
n_sys
¶ The number of distinct systems participating in this
Cycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.n_sys
8
 Type
int

property
gates
¶ The
dict
ofGate
s in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.gates
{(0, 1): Gate.cx, (2,): Gate.t, (3,): Gate.t, (4,): Gate.t}
 Type
dict

property
gates_single
¶ The singlequbit gates of this cycle; a
dict
ofGate
s in thisCycle
that act on one subsystem.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.gates_single
{(2,): Gate.t, (3,): Gate.t, (4,): Gate.t}
 Type
dict

property
gates_multi
¶ The multiqubit gates of this cycle; a
dict
ofGate
s in thisCycle
that act on at least two subsystems.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.gates_multi
{(0, 1): Gate.cx}
 Type
dict

property
gate_labels
¶ A sorted tuple of all labels in the cycle that have gates (i.e. labels that point to a
Gate
operation).import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.gate_labels
(0, 1, 2, 3, 4)
 Type
list

property
prep
¶ The
dict
ofPrep
s in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.prep
{(6,): Prep()}
 Type
dict

property
prep_labels
¶ A sorted tuple of all labels in the cycle that are a preparation (i.e. labels that point to a
Prep
operation).import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.prep_labels
(6,)
 Type
list

property
meas
¶ The
dict
ofMeas
s in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.meas
{(5,): Meas()}
 Type
dict

property
meas_labels
¶ A sorted tuple of all labels in the cycle that are a preparation (i.e. labels that point to a
Meas
operation).import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.meas_labels
(5,)
 Type
list

property
block
¶ The
dict
ofBlock
s in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.block
{(7,): Block()}
 Type
dict

property
block_labels
¶ A sorted tuple of all labels in the cycle that are blocked (i.e. labels that point to a
Block
operation).import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.block_labels
(7,)
 Type
list

property
operations
¶ The total
dict
ofOperation
s in thisCycle
.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cnot, (2, 3, 4): tq.Gate.t, 5: tq.Meas(), 6: tq.Prep(), 7: tq.Block()}) cycle.operations
{(0, 1): Gate.cx, (2,): Gate.t, (3,): Gate.t, (4,): Gate.t, (6,): Prep(), (5,): Meas(), (7,): Block()}
 Type
dict

tensor
(labels=None)¶ Returns a
Tensor
representation of theGate
s in thisCycle
.The matrix representation (which will be exponentially sized in the number of systems!) of this cycle can be obtained with:
import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cx, 2: tq.Gate.i}) matrix = cycle.tensor().mat() tq.visualization.plot_mat(matrix)
 Parameters
labels (
list
) – An optional list of labels to force into the tensor in case they are not already present as labels in this cycle. Return type

is_equivalent
(other, ignore_imm=True, ignore_id=True)¶ Checks if this cycle is equivalent to another cycle.
import trueq as tq cycle1 = tq.Cycle({(0, 1): tq.Gate.cx}, immutable=True) cycle2 = tq.Cycle({(0, 1): tq.Gate.cx}, immutable=False) cycle1.is_equivalent(cycle2)
True
 Parameters
 Return type
bool

to_dict
()¶ Returns a dictionary representation of a
Cycle
object. This dictionary representation contains only base Python classes.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cx}, immutable=True) cycle.to_dict()
{'content': {(0, 1): ('Gate', {'u': (((1+0j), 0j, 0j, 0j), (0j, (1+0j), 0j, 0j), (0j, 0j, 0j, (1+0j)), (0j, 0j, (1+0j), 0j))})}, 'immutable': True}
 Return type
dict

static
from_dict
(dic)¶ Returns a
Cycle
constructed from a dictionary representation.import trueq as tq cycle = tq.Cycle({(0, 1): tq.Gate.cx}, immutable=True) cycle.from_dict(cycle.to_dict())
imm (0, 1): Gate.cx Name:

 Gate.cx
 Aliases:

 Gate.cx
 Gate.cnot
 Likeness:

 CNOT
 Generators:

 'IX': 90.0
 'ZI': 90.0
 'ZX': 90.0
 Matrix:


pop
(item)¶ Deletes the operation from this cycle that acts on the given label tuple and returns it.
import trueq as tq cycle = tq.Cycle({(0,): tq.Gate.x, (1,): tq.Meas()}) print(cycle.pop(0)) cycle
Gate.x
(1): Meas() Name:

 Meas()
 Parameters
item (
tuple
) – A tuple of qubit labels. Raises
KeyError – If no operation acts on the given label tuple.
ValueError – If the cycle is immutable.