Base Module¶
The squint.ops.base module contains the core abstractions for building quantum circuits in Squint.
Key Concepts¶
Wires and Degrees of Freedom¶
A Wire represents a quantum subsystem with a specific Hilbert space dimension. Each wire can optionally specify a degree of freedom (DoF) type to distinguish between different physical encodings:
- DV - Discrete variable systems (qubits, qudits)
- CV - Continuous variable systems (optical modes in Fock space)
- TimeBin, FreqBin, Spatial - Photonic encoding schemes
Operation Hierarchy¶
All quantum operations inherit from AbstractOp:
- States:
AbstractPureState,AbstractMixedState- Initial quantum states - Gates:
AbstractGate- Unitary transformations - Channels:
AbstractKrausChannel,AbstractErasureChannel- Non-unitary operations - SharedGate - Parameter sharing across multiple wires (e.g., for phase estimation)
- Block - Grouping multiple operations
Typical Usage¶
from squint.ops.base import Wire, DV, SharedGate
# Create qubit wires
q0 = Wire(dim=2, dof=DV, idx=0)
q1 = Wire(dim=2, dof=DV, idx=1)
# Use in operations
from squint.ops.dv import DiscreteVariableState, RZGate
state = DiscreteVariableState(wires=(q0,), n=(0,))
phase = RZGate(wires=(q0,), phi=0.0)
AbstractDoF
¶
Bases: Module
Abstract base class for degrees of freedom (DoF) in quantum systems.
Degrees of freedom classify the physical encoding of quantum information. Each DoF subclass represents a different physical implementation for encoding quantum states, which can be useful for enforcing circuit validity and distinguishing between different physical platforms.
Subclasses
DV: Discrete variable systems (qubits, qudits) CV: Continuous variable systems (optical modes) TimeBin: Time-bin encoded photonic systems FreqBin: Frequency-bin encoded photonic systems Spatial: Spatial mode encoding
Source code in src/squint/ops/base.py
DV
¶
Bases: AbstractDoF
Discrete variable degree of freedom.
Represents finite-dimensional quantum systems such as qubits (dim=2) or qudits (dim>2). These systems have a finite number of basis states and are commonly implemented in platforms like superconducting circuits, trapped ions, and spin systems.
Source code in src/squint/ops/base.py
CV
¶
Bases: AbstractDoF
Continuous variable degree of freedom.
Represents infinite-dimensional Fock space systems, typically optical modes with photon number states. In practice, the Hilbert space is truncated at a finite photon number cutoff specified by the wire dimension.
Source code in src/squint/ops/base.py
TimeBin
¶
Bases: AbstractDoF
Time-bin encoded degree of freedom.
Represents photonic qubits/qudits encoded in discrete time bins. Information is encoded in the arrival time of single photons, commonly used in fiber-based quantum communication.
Source code in src/squint/ops/base.py
FreqBin
¶
Bases: AbstractDoF
Frequency-bin encoded degree of freedom.
Represents photonic qubits/qudits encoded in discrete frequency modes. Information is encoded in the spectral properties of photons, useful for wavelength-division multiplexing in quantum networks.
Source code in src/squint/ops/base.py
Spatial
¶
Bases: AbstractDoF
Spatial mode encoded degree of freedom.
Represents quantum information encoded in spatial modes of light, such as different paths in an interferometer or transverse spatial modes (e.g., orbital angular momentum modes).
Source code in src/squint/ops/base.py
Wire
¶
Bases: Module
Represents a quantum subsystem (wire) in a circuit.
A Wire defines a single quantum subsystem with a specific Hilbert space dimension and degree of freedom type. Wires are the fundamental building blocks that connect quantum operations in a circuit, determining how operators act on different parts of the composite quantum system.
Attributes:
| Name | Type | Description |
|---|---|---|
dim |
int
|
The dimension of the local Hilbert space. For qubits, dim=2; for qudits, dim>2; for Fock spaces, dim is the photon number cutoff. |
dof |
type[AbstractDoF]
|
The type of degree of freedom this wire represents (e.g., DV for discrete variable, CV for continuous variable). |
idx |
str | int
|
Unique identifier for the wire, used to track which operations act on which subsystems. |
Example
from squint.ops.base import Wire, DV, CV
# Create a qubit wire
qubit = Wire(dim=2, dof=DV, idx=0)
# Create an optical mode wire with photon cutoff of 5
mode = Wire(dim=5, dof=CV, idx="signal")
# Use wires in operations
from squint.ops.dv import DiscreteVariableState, HGate
state = DiscreteVariableState(wires=(qubit,), n=(0,))
gate = HGate(wires=(qubit,))
Source code in src/squint/ops/base.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
__init__(dim: int, dof: Optional[type[AbstractDoF]] = AbstractDoF, idx: Optional[int | str] = None)
¶
Initialize a Wire.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
The dimension of the local Hilbert space. Must be >= 2. For qubits use dim=2, for qudits use dim>2, for Fock spaces this is the photon number cutoff. |
required |
dof
|
type[AbstractDoF]
|
Type of degree of freedom that this wire represents. Can be used to enforce circuit validity and distinguish between different physical encodings. Defaults to AbstractDoF. |
AbstractDoF
|
idx
|
str | int
|
Unique identifier for the wire. If not provided, a random id is generated. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If dim < 2. |
Source code in src/squint/ops/base.py
AbstractOp
¶
Bases: Module
An abstract base class for all quantum objects, including states, gates, channels, and measurements. It provides a common interface for various quantum objects, ensuring consistency and reusability across different types of quantum operations.
Attributes:
| Name | Type | Description |
|---|---|---|
wires |
tuple[int, ...]
|
A tuple of nonnegative integers representing the quantum wires on which the operation acts. Each wire corresponds to a specific subsystem in the composite quantum system. |
Source code in src/squint/ops/base.py
__init__(wires: Sequence[Wire])
¶
Initializes the AbstractOp instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wires
|
tuple[int, ...]
|
A tuple of nonnegative integers representing the quantum wires on which the operation acts. Defaults to (0, 1). |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any wire in the provided tuple is not a nonnegative integer. |
Source code in src/squint/ops/base.py
unwrap()
¶
A base method for unwrapping an operator into constituent parts, important in, e.g., shared weights across operators.
This method can be overridden by subclasses to provide additional unwrapping functionality, such as decomposing composite operations into their components.
Returns:
| Name | Type | Description |
|---|---|---|
ops |
tuple[AbstractOp]
|
A tuple of AbstractOp which represent the constituent ops. |
Source code in src/squint/ops/base.py
AbstractState
¶
Bases: AbstractOp
An abstract base class for all quantum states.
Source code in src/squint/ops/base.py
AbstractPureState
¶
Bases: AbstractState
An abstract base class for all pure quantum states, equivalent to the state vector formalism.
Pure states are associated with a Hilbert space of size
\(|\psi\rangle \in \mathcal{H}^{d_1 \times \dots \times d_w}\)
where \(w\) = len(wires) and \(d\) is assigned at compile time.
Source code in src/squint/ops/base.py
AbstractMixedState
¶
Bases: AbstractState
An abstract base class for all mixed quantum states, equivalent to the density matrix formalism.
Mixed states are associated with a Hilbert space of size
\(\rho \in \mathcal{H}^{d_1 \times \dots \times d_w \times d_1 \times \dots \times d_w}\)
where \(w\) = len(wires) and \(d\) is assigned at compile time.
Source code in src/squint/ops/base.py
AbstractGate
¶
Bases: AbstractOp
An abstract base class for all unitary quantum gates, which transform an input state in a reversible way.
\(U \in \mathcal{H}^{d_1 \times \dots \times d_w \times d_1 \times \dots \times d_w}\)
where \(w\) = len(wires) and \(d\) is assigned at compile time.
Source code in src/squint/ops/base.py
AbstractChannel
¶
Bases: AbstractOp
An abstract base class for quantum channels, including channels expressed as Kraus operators, erasure (partial trace), and others.
Source code in src/squint/ops/base.py
AbstractMeasurement
¶
Bases: AbstractOp
An abstract base class for quantum measurements. Currently, this is not supported, and measurements are projective measurements in the computational basis.
Source code in src/squint/ops/base.py
SharedGate
¶
Bases: AbstractGate
A class representing a shared quantum gate, which allows for the sharing of parameters or attributes across multiple copies of a quantum operation. This is useful for scenarios where multiple gates share the same underlying structure or parameters, such as in variational quantum circuits. This is most commonly used when applying the same parameterized gate across different wires, e.g., phase gates, for studying phase estimation protocols.
Attributes:
| Name | Type | Description |
|---|---|---|
op |
AbstractOp
|
The base quantum operation that is shared across multiple copies. |
copies |
Sequence[AbstractOp]
|
A sequence of copies of the base operation, each acting on different wires. |
where |
Callable
|
A function that determines which attributes of the operation are shared across copies. |
get |
Callable
|
A function that retrieves the shared attributes from the base operation. |
Source code in src/squint/ops/base.py
unwrap()
¶
Unwraps the shared ops for compilation and contractions.
AbstractKrausChannel
¶
Bases: AbstractChannel
An abstract base class for quantum channels expressed as Kraus operators.
The channel \(K\) is of shape \((d_1 \times \dots \times d_w \times d_1 \times \dots \times d_w)\)
where \(w\) = len(wires) and \(d\) is assigned at compile time.
Source code in src/squint/ops/base.py
AbstractErasureChannel
¶
Bases: AbstractChannel
This channel traces out the local Hilbert space associated with the wires
Source code in src/squint/ops/base.py
Block
¶
Bases: Module
A block operation that groups a sequence of quantum operations.
Blocks allow organizing multiple operations into a single logical unit.
They can be nested within circuits or other blocks, and support the same
add and unwrap interface as Circuit. Unlike Circuit, Block does not
specify a backend and is purely for organizational purposes.
Attributes:
| Name | Type | Description |
|---|---|---|
ops |
OrderedDict
|
Ordered dictionary mapping keys to operations or nested blocks. |
Example
Source code in src/squint/ops/base.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 | |
wires: Sequence[Wire]
property
¶
Get all wires used by operations in this block.
Returns:
| Type | Description |
|---|---|
Sequence[Wire]
|
set[Wire]: Set of all Wire objects that operations in this block act on. |
__init__()
¶
Initialize an empty Block.
Creates a new Block with no operations. Operations can be added
using the add method.
add(op: Union[AbstractOp, Block], key: str = None) -> None
¶
Add an operator to the block.
Operators are added sequentially. When this block is used in a circuit, the operations will be applied in the order they were added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
op
|
AbstractOp | Block
|
The operator or nested block to add. |
required |
key
|
str
|
A string key for indexing into the block's ops dictionary. If None, an integer counter is used as the key. |
None
|
Source code in src/squint/ops/base.py
unwrap() -> tuple[AbstractOp]
¶
Unwrap all operators in the block into a flat tuple.
Recursively calls unwrap() on all contained operations and nested
blocks to produce a flat sequence of atomic operations.
Returns:
| Type | Description |
|---|---|
tuple[AbstractOp]
|
tuple[AbstractOp]: Flattened tuple of all operations in order. |
Source code in src/squint/ops/base.py
create(dim)
cached
¶
Returns the create operator for a Hilbert space of dimension dim.
The create operator is a matrix that adds one excitation to a quantum system,
effectively increasing the energy level by one.
Args:
dim (int): The dimension of the Hilbert space.
Returns:
jnp.ndarray: A 2D array of shape (dim, dim) representing the create operator.
Source code in src/squint/ops/base.py
destroy(dim)
cached
¶
Returns the destroy operator for a Hilbert space of dimension dim.
The destroy operator is a matrix that annihilates one excitation of a quantum system,
effectively reducing the energy level by one.
Args:
dim (int): The dimension of the Hilbert space.
Returns:
jnp.ndarray: A 2D array of shape (dim, dim) representing the destroy operator.
Source code in src/squint/ops/base.py
eye(dim)
cached
¶
Returns the identity operator for a Hilbert space of dimension dim.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
The dimension of the Hilbert space. |
required |
Returns: jnp.ndarray: A 2D array of shape (dim, dim) representing the identity operator.
Source code in src/squint/ops/base.py
bases(dim)
cached
¶
Returns the computational basis indices for a Hilbert space of dimension dim.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
The dimension of the Hilbert space. |
required |
Returns:
| Type | Description |
|---|---|
|
jnp.ndarray: A 1D array of shape (dim,) containing the indices of the computational bases. |
Source code in src/squint/ops/base.py
dft(dim)
cached
¶
Returns the discrete Fourier transform matrix of dimension dim.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
The dimension of the DFT matrix. |
required |
Returns:
| Type | Description |
|---|---|
|
jnp.ndarray: A 2D array of shape (dim, dim) representing the DFT matrix. |
Source code in src/squint/ops/base.py
basis_operators(dim)
cached
¶
Return a basis of orthogonal Hermitian operators on a Hilbert space of dimension \(d\), with the identity element in the last place. i.e., the Gell-Mann operators (for dim=2, these are the four Pauli operators).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
The dimension of the Hilbert space. |
required |
Returns:
| Type | Description |
|---|---|
|
jnp.ndarray: A 3D array of shape (n_operators, dim, dim) containing the basis operators. |