paulicirc.gadgets
Pauli gadgets.
CommutationCode
- CommutationCode
A number 0-7 describing how two Pauli gadgets are to be commuted past each other. See
Gadget.commute_pastfor a description of the commutation procedure and associated commutation code conventions.
Gadget
- final class Gadget(data, num_qubits=None, *, _ephemeral=False)[source]
Bases:
objectA Pauli gadget.
- static assemble_data(legs, phase)[source]
Assembles gadget data from the given legs and phase.
- Parameters:
legs (
PauliArray)phase (
PhaseLike)
- Return type:
- commute_past(other, code)[source]
Commutes this gadget past the given gadget, using the given
CommutationCodeto determine how the gadgets are to be commuted.If
code=0, the gadgets are not commuted:p.commute_past(q, 0) # -> (p, q, None)
If the gadgets have even overlap, commutation always swaps them, regardless of the commutation code:
# p.overlap(q) % 2 == 0 p.commute_past(q, code) # -> (q, p, None)
If the gadgets have odd overlap, commutation codes 1-7 correspond to the six possible ways to commute them, each resulting in a gadget triple:
# p.overlap(q) % 2 != 0 # code in range(1, 8) p.commute_past(q, code) # -> (r, s, t)
The following mathematical procedure is used to compute the triple
(r, s, t)of gadgets obtained by commuting(p, q). Note that gadget ordering is read left-to-right, so that(p, q)means “p first, q second” and(r, s, t)means “r first, s second, r third”; this is the opposite convention to matrix multiplication, which is read right-to-left instead.The gadgets
(p, q)are simultaneously mapped, by means of a suitable Clifford circuit, to X and Z rotations on qubit 0. The rotations are either(x(0, p.phase), z(0, q.phase))or(z(0, p.phase), x(0, q.phase)), depending on whether there is an even or odd number of occurrences, respectively, of ZX, XY or YZ pairs in corresponding legs ofpandq.The X and Z rotations on qubit 0 are commuted past each other in one of six possible ways, described below and corresponding to commutation codes 1-7. This results in a sequence of three Pauli rotations on qubit 0, chosen between X, Y and Z rotations.
The three Pauli rotations on qubit 0 are simultaneously mapped back to gadgets
(r, s, t)by the inverse of the Clifford circuit from Step 1.
The 6 possible commutations for Step 2 correspond to commutation codes 1-7 as follows, each commutation appearing in one of two flavours depending on whether Step 1 resulted in
(rx, rz)or(rz, rx).Code 1:
(x, z) -> (z, y, z)and(z, x) -> (x, y, x)Code 2:
(x, z) -> (y, z, y)and(z, x) -> (y, x, y)Code 3:
(x, z) -> (x, y, x)and(z, x) -> (z, y, z)Code 4:
(x, z) -> (y, x, y)and(z, x) -> (y, z, y)Code 5:
(x, z) -> (z, x, y)and(z, x) -> (x, z, y)Code 6:
(x, z) -> (z, y, x)and(z, x) -> (x, y, z)Code 7:
(x, z) -> (y, z, x)and(z, x) -> (y, x, z)
- commutes_with(other)[source]
Returns whether this gadget commutes with the given gadget, i.e. whether the overlap is even.
- classmethod from_legs(legs, phase)[source]
Returns the gadget with given legs and phase.
- Parameters:
legs (
PauliArray)phase (
PhaseLike)
- Return type:
- classmethod from_paulistr(paulistr, phase)[source]
Returns the gadget with given legs (as paulistr) and phase.
- classmethod from_sparse_paulistr(paulistr, qubits, num_qubits, phase)[source]
Returns the gadget with given legs (as a sparse paulistr) and phase.
- property legs
Legs of the gadget.
- Return type:
- static legs_from_paulistr(paulistr)[source]
Returns the legs corresponding to the given paulistr.
- Parameters:
paulistr (
str)- Return type:
- static legs_from_sparse_paulistr(paulistr, qubits, num_qubits)[source]
Returns the legs corresponding to the given sparse paulistr.
- Parameters:
- Return type:
- overlap(other)[source]
Returns the overlap between the legs of this gadgets and those of the given gadget, computed as the number of qubits where the legs of the two gadgets differ and are both not the identity Pauli (the value 0, as a
Pauli).
- static phase2frac(phase)[source]
Converts a phase to a fraction of \(\pi\). The number of bits of precision is controlled by the value of
options.display_prec: this is set to 8 by default, corresponding to multiples of \(\pi/256\).
- property phase_frac
Exact representation of the gadget phase as a fraction of \(\pi\).
- Return type:
- property phase_str
String representation of the gadget phase, as a fraction of \(\pi\).
- Return type:
- classmethod random(num_qubits, *, allow_zero=True, allow_legless=True, rng=None)[source]
Returns a gadget with uniformly sampled legs and phase.
- statevec(input, canonical_phase=False)[source]
Computes the statevector resulting from the application of this gadget to the given input statevector.
- Parameters:
input (
ComplexArray1D|FloatArray1D)canonical_phase (
bool; default =False)
- Return type:
GadgetData
PAULI_CHARS
PAULI_MATS
- PAULI_MATS = (array([[1.+0.j, 0.+0.j], [0.+0.j, 1.+0.j]]), array([[0.+0.j, 1.+0.j], [1.+0.j, 0.+0.j]]), array([[ 1.+0.j, 0.+0.j], [ 0.+0.j, -1.+0.j]]), array([[ 0.+0.j, -0.-1.j], [ 0.+1.j, 0.+0.j]]))
The four Pauli matrices.
PHASE_NBYTES
- PHASE_NBYTES = 8
Number of bytes used for phase representation.
Pauli
PauliArray
PauliArray2D
PauliChar
Phase
- Phase
Type alias for a phase, as a 64-bit float.
PhaseArray
PhaseDataArray
PhaseLike
QubitIdx
- QubitIdx
Type alias for the index of a qubit.
QubitIdxs
are_same_phase
are_same_phases
- are_same_phases(lhs, rhs)[source]
Low-level function. Whether the given phase arrays are deemed to be the same.
- Parameters:
lhs (
PhaseArray)rhs (
PhaseArray)
- Return type:
broadcast_idxs
- broadcast_idxs(*idxs_seq)[source]
Low-level function. Broadcasts any number of sequences of qubit indexes to a common length, defined as the least common multiple of lengths for index sequences. Individual qubit indexes passed to this function are considered the same as singleton index sequences. If any sequence is empty, the returned sequences are all empty.
commute_gadget_pair
- commute_gadget_pair = CPUDispatcher(<function commute_gadget_pair>)[source]
Low-level function. Used by
Gadget.commute_pastto commute a pair of gadgets. It operates on a single array, containing the linearised data for the two gadgets to be commuted, as well as auxiliary space for a third gadget. It expects data for the third gadget to be set to zero, except for the desired commutation code (cf.CommutationCode) which should be written on the last byte.
decode_phases
encode_phases
gadget_data_len
gadget_overlap
get_gadget_leg_at
get_gadget_legs
- get_gadget_legs = CPUDispatcher(<function get_gadget_legs>)[source]
Low-level function. Extract an array of leg information from given gadget data. The returned array has values in
range(4), where the encoding is explained inGadgetData.
get_gadget_legs_at
get_phase
invert_phases
is_zero_phase
pauli_product_phase
set_gadget_leg_at
set_gadget_legs
- set_gadget_legs(g, legs)[source]
Low-level function. Sets leg information in the given gadget data. The input array should have values in
range(4), where the encoding is explained inGadgetData.- Parameters:
g (
GadgetData)legs (
PauliArray)
- Return type: