Welcome to QutiePy’s documentation!

Package Description

QutiePy is a python3 package designed to provide an easy and practical toolset for simulated quantum computing aimed users with any experience level in quantum theory and a working knowledge of pythonic OOP.

QutiePy is currently a work-in-progress and is being developed by a first-timer. Please report any issues or suggestions on the GitHub repo at <https://github.com/franklinscudder/QutiePy/issues>.

Reference

General Notes and Conventions

  • QutiePy assumes that the first bit in a register (reg[0]) is the least significant bit. Thus, if a register is observed as [0,0,1] the resulting output would be 4.

  • For default controlled gates, the first bit (LSB) is the control bit.

  • The names used in the package use camelCase as much as possible with the caveat that ‘N’ as in ‘number of’ is capitalised and treated as a word itself (NBits, NStates).

  • For a pretty representation of a register, just call print(reg).

  • When calling a gate on a register, the result is returned in a new object, the original register is not modified in any way.

  • All gate objects implement a __call__ method and are intended to be applied to a register in this way:

# A Hadamard gate
h = hadamard(4)

# A 4-bit register
r = register(4)

# Applying the gate to the register
result = h(r)
  • Gates can be compounded is series as a new object. a(b(c)) will return a compoundGate object if a, b and c are gates.

  • If reproducability is required, setSeed() can be used to set the RNG seed to a given string so that each run will produce the same results.

QutiePy

qutiepy.py

The main file containing the core components of QutiePy, including classes for gates and the register class as well as a few functions which handle on gate and register objects:

class qutiepy.register(NBits)

Bases: object

N-bit quantum register. The initial state is (1+0j)|0>.

Parameters

NBits (int) – Size of the register in bits.

NBits

Number of bits in the register.

Type

int

NStates

Number of states the register can occupy (2^NBits).

Type

int

amps

The complex probability amplitudes of each state of the register.

Type

list

probabilities()

Return the probability associated with observing each state.

Returns

probabilities – The probabilities p(X=x_i) of observing each state.

Return type

numpy array

observe(bit=- 1, collapseStates=True, fmt='int')

‘Observe’ the register and return an integer representation of the observed state according to the probability amplitudes.

If a bit argument is supplied, only that bit will be observed and it’s state returned as an integer (1 or 0).

If collapseStates=True, adjust the amplitudes to reflect the collapsed state.

Parameters
  • bit (int, optional) – The index of the bit to be observed.

  • collapseStates (bool, optional) – Flag to set whether probability amplitudes will be affected by this observation.

  • fmt (string, optional) – One of “int” (default), “bin” or “hex”

Returns

state – The observed state of the register or bit.

Return type

int or string

prod(B)

Return the tensor product of self and B, ‘Joining’ two registers into a single larger register with self at the MSB and ‘B’ at the LSB.

Parameters

B (register) – The register to be appended to self.

Returns

result – The resulting joined register.

Return type

register

See also

prod

Equivalent function

bloch(eps=1e-12)

Return the angles theta and phi used in the Bloch sphere representation of a single qubit register (eps is used to avoid infinite and NaN results, set to 0 to disable).

Returns

  • theta (float) – The angle theta = 2*arccos(amps[0]).

  • phi (float) – The angle phi = Ln[(amps[1]+eps)/(sin(theta/2)+eps)].

density()

Return the density matrix of the register.

Returns

density – The density matrix.

Return type

numpy array

reducedPurities()

Return the reduced purity of each bit of the register, i.e.: Tr[Tr_i(D)^2] where D is the full density matrix of the register and Tr_i is the partial trace over the subspace of bit index ‘i’.

Returns

purities – The reduced purity of each qubit in the register.

Return type

numpy array

setAmps(amps)

Manually set the qubit probability amplitudes in-place, ensuring they remain properly normalised.

Parameters

amps (iterable) – The relative complex amplitudes to be applied to the register with normalisation.

qutiepy.prod(regA, regB)

‘Join’ two registers into a single larger register with regA at the MSB and regB at the LSB by performing the Kronecker product between their state vectors |A>|B> = |AB>.

Parameters
  • regA (register) – The register to be placed at the MSB.

  • regB (register) – The register to be placed at the LSB.

Returns

result – The resulting joined register.

Return type

register

See also

register.prod

Equivalent class method.

class qutiepy.genericGate(NBits)

Bases: object

Base class for callable quantum logic gates.

Parameters

NBits (int) – Size of the registers that the gate will take as input/output.

NBits

Number of bits that the gate takes as input/output.

Type

int

matrix

The 2^NBits by 2^NBits matrix representation of the gate.

Type

2D numpy array

NControlBits

Indicates whether a control bit has been added using .addControlBits().

Type

bool

isInverse

Indicates whether the object has been created by a call to H().

Type

bool

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

class qutiepy.compoundGate(NBits)

Bases: qutiepy.genericGate

A class returned when gates are compounded. See genericGate attributes.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.hadamard(NBits, skipBits=[])

Bases: qutiepy.genericGate

A callable hadamard gate object.

Parameters
  • NBits (int) – Number of bits that the gate takes as input/output.

  • skipBits (list of int, optional, deprecated) – Indices of bits in a register that this gate will not operate on.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.phaseShift(NBits, phi, skipBits=[])

Bases: qutiepy.genericGate

A callable phase-shift gate object.

Parameters
  • NBits (int) – Number of bits that the gate takes as input/output.

  • phi (float) – The phase angle through which to shift the amplitudes.

  • skipBits (list of int, optional, deprecated) – Indices of bits in a register that this gate will not operate on.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.pauliX(NBits, skipBits=[])

Bases: qutiepy.genericGate

A callable Pauli-X gate object, AKA the quantum NOT gate.

Parameters
  • NBits (int) – Number of bits that the gate takes as input/output.

  • skipBits (list of int, optional) – Indices of bits in a register that this gate will not operate on.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.pauliY(NBits, skipBits=[])

Bases: qutiepy.genericGate

A callable Pauli-Y gate object.

Parameters
  • NBits (int) – Number of bits that the gate takes as input/output.

  • skipBits (list of int, optional, deprecated) – Indices of bits in a register that this gate will not operate on.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.pauliZ(NBits, skipBits=[])

Bases: qutiepy.phaseShift

A callable Pauli-Z gate object.

Parameters
  • NBits (int) – Number of bits that the gate takes as input/output.

  • skipBits (list of int, optional, deprecated) – Indices of bits in a register that this gate will not operate on.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.cNot

Bases: qutiepy.genericGate

A callable CNOT gate object.

The first bit (LSB) in the register on which this gate is called is the control bit.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.ccNot

Bases: qutiepy.genericGate

A callable CCNOT (Toffoli) gate object.

The first two bits (LSBs) in the register on which this gate is called are the control bits.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.swap

Bases: qutiepy.genericGate

A callable SWAP gate object. For two qubits in a register (A,B), outputs (B,A).

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.sqrtSwap

Bases: qutiepy.genericGate

A callable sqrt(SWAP) gate object.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.sqrtNot(NBits, skipBits=[])

Bases: qutiepy.genericGate

A callable sqrt(not) or sqrt(Pauli-X) gate object.

Parameters
  • NBits (int) – Number of bits that the gate takes as input/output.

  • skipBits (list of int, optional, deprecated) – Indices of bits in a register that this gate will not operate on.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.fredkin

Bases: qutiepy.genericGate

A callable Fredkin (CCSWAP) gate object.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.identity(NBits)

Bases: qutiepy.genericGate

A callable identity gate object.

Parameters

NBits (int) – Number of bits that the gate takes as input/output.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.QFT(NBits)

Bases: qutiepy.genericGate

A callable quantum Fourier transform (QFT) gate object.

Parameters
  • NBits (int) – Number of bits that the gate takes as input/output.

  • omega (complex) –

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

class qutiepy.parallelGate(gates)

Bases: qutiepy.genericGate

A gate class to combine gates in parallel.

Parameters

gates (array of gate objects) – The gates to be combined in parallel, with the first gate acting on the LSB of an applied register.

H()
Return an inverse copy of self, i.e. a gate whose matrix representation is

the Hermitian adjoint of self.matrix.

Returns

gate – The gate performing the inverse operation of self.

Return type

Gate Object

addControlBits(controlOffsets)

Add control bits to this single bit gate.

Parameters

controlOffsets (array of int != 0) – The bit index offsets relative to this gate to control off of.

Returns

result – The resulting controlled gate.

Return type

gate object

inverse()

Alias of self.H().

See also

genericGate.H

This method maps to self.H()

qutiepy.serialGate(gates)

Combine an iterable of gate objects in series.

Parameters

gates (iterable of gate objects) – The gate objects to be combined in serial. The operator to be applied first should be at index 0.

Returns

comp – The result of combining the supplied operators in series.

Return type

compoundGate object

qutiepy.setSeed(seed)

Set the RNG seed for reproducibility.

Parameters

seed (string) – The seed to be used by the RNG.

Returns

result – True if successful.

Return type

bool

Usage Example

This script demonstrates the basic usage of the module, constructing the Bell state ‘Phi+’. A Bell state is a pair of qubits which exhibit maximal entanglement. They are the simplest example of this phenomenon.

This script can be found in the examples folder of your package installation.

import qutiepy as qu

# Create two 1-qubit registers, initialised to 0 by default
r1 = qu.register(1)
r2 = qu.register(1)

# Create a Hadamard and a CNOT gate
h = qu.hadamard(1)
cn = qu.cNot()

# Apply the hadamard operation to r1
r1 = h(r1)

# 'Join' the two bits together into a single register
r = qu.prod(r1,r2)

# Apply the CNOT gate to the new register
r = cn(r)

# Analyse the resulting Bell state
print("The state vector of r:")
print(r)
print()
print("The probabilities of observing each state of r:")
print(r.probabilities())
print()
print("The reduced purities of each qubit in r (0.5 = maximally entangled, 1 = fully unentangled):")
print(r.reducedPurities())
print()
print("The result of observing ten versions of the Bell state r:")
for i in range(10):
    print(r.observe(collapseStates=False)),

Entanglement, Parallel Gates and skipBits

Entanglement in QutiePy is handled by keeping entangled qubits in the same register as each other. Thus, once registers are combined using prod(), they cannot be separated. The best way to apply a gate operation to only some bits in a register, you can use a parallelGate object composed of any operations you wish to apply to the gate, along with identity gates acting on bits you wish to remain unaffected.

_images/Example2.png

A gate can also be specified with a list skipBits of qubit indices to be left unaltered by the gate. This is no longer the recommended method and is only retained for compatability reasons. The example below shows how this can be used to transform a register which has been placed in a state of entanglement.

_images/Example1.png

Indices and tables