Qubit – Qubits and quantum systems

class squanch.qubit.QSystem(num_qubits, index=None, state=None)[source]

Bases: object

Represents a multi-body, maximally-entangleable quantum system. Contains references to constituent qubits and (if applicable) its parent QStream. Quantum state is represented as a density matrix in the computational basis.

__init__(num_qubits, index=None, state=None)[source]

Instatiate the quantum state for an n-qubit system

Parameters:
  • num_qubits (int) – number of qubits in the system, treated as maximally entangled
  • index (int) – index of the QSystem within the parent QStream
  • state (np.array) – density matrix representing the quantum state. By default, |000...0><0...000| is used
__weakref__

list of weak references to the object (if defined)

apply(operator)[source]

Apply an N-qubit unitary operator to this system’s N-qubit quantum state

Parameters:operator (np.array) – the unitary N-qubit operator to apply
Returns:nothing, the qsystem state is mutated
classmethod from_stream(qstream, index)[source]

Instantiate a QSystem from a given index in a parent QStream

Parameters:
  • qstream (QStream) – the parent stream
  • index (int) – the index in the parent stream corresponding to this system
Returns:

the QSystem object

measure_qubit(index)[source]

Measure the qubit at a given index, partially collapsing the state based on the observed qubit value. The state vector is modified in-place by this function.

Parameters:index (int) – the qubit to measure
Returns:the measured qubit value
qubit(index)[source]

Access a qubit by index; self.qubits does not instantiate all qubits unless casted to a list. Use this function to access a single qubit of a given index.

Parameters:index (int) – qubit index to generate a qubit instance for
Returns:the qubit instance
class squanch.qubit.Qubit(qsystem, index)[source]

Bases: object

A wrapper class representing a single qubit in an existing quantum system.

__init__(qsystem, index)[source]

Instantiate the qubit from an existing QSystem and index

Parameters:
  • qsystem (QSystem) – n-qubit quantum system that this qubit points to
  • index (int) – particle index in the quantum system, ranging from 0 to n-1
__weakref__

list of weak references to the object (if defined)

apply(operator, id=None)[source]

Apply a single-qubit operator to this qubit, tensoring with I and passing to the qsystem.apply() method

Parameters:
  • operator (np.array) – a single qubit (2x2) complex-valued matrix
  • cacheID (str) – a character or string to cache the expanded operator by (e.g. Hadamard qubit 2 -> “IHII…”)
classmethod from_stream(qstream, system_index, qubit_index)[source]

Instantiate a qubit from a parent stream (via a QSystem call)

Parameters:
  • qstream (QStream) – the parent stream
  • system_index (int) – the index corresponding to the parent QSystem
  • qubit_index (int) – the index of the qubit to be recalled
Returns:

the qubit

measure()[source]

Measure a qubit, modifying the density matrix of its parent QSystem in-place

Returns:the measured value
serialize()[source]

Generate a reference to reconstruct this qubit from shared memory

Returns:qubit reference as (systemIndex, qubitIndex)