Quantum
Quantum realizations shared by the physics packages.
The package holds the Hilbert space of a subsystem, qubits and bosonic modes, the generator and dissipator laws, the compiler and the solvers of their dynamics, and it measures operator systems: their spectra, ground states, purity, and entanglement.
DissipatorLaw(operator, rate=1.0, *, label='')
dataclass
Bases: BaseLaw
A law that adds one Lindblad term, a collapse operator and its rate.
An open system loses coherence to its surroundings. The Lindblad master equation writes that loss as a sum of terms,
each with a collapse operator L and a rate. The law adds rate * (L rho L^† - (L^† L rho + rho L^† L) / 2) to the
rate of the density matrix rho.
Unlike GeneratorLaw, which adds a reversible part to the evolution, it adds an irreversible part that drives the
state toward a mixture. Choose it when the system exchanges energy or information with an environment.
It appears in quantum optics, where an atom decays by spontaneous emission and a cavity leaks photons, and in quantum computing, where qubits lose their phase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operator
|
object
|
The collapse operator |
required |
rate
|
object
|
The non-negative rate, a number or a Sage expression of parameters and time. |
1.0
|
label
|
str
|
The name of the law, or the empty string for a law without one. |
''
|
Examples:
>>> from sage.all__sagemath_symbolics import SR, var
>>> from skcomplex.systems.physics.common.quantum import DissipatorLaw
>>> from skcomplex.core.components.laws.utils import local_operator
>>> minus = local_operator(SR.symbol('q'), SR.symbol('minus'))
>>> DissipatorLaw(minus, 2 * var('gamma')).rate
2*gamma
GeneratorLaw(operator, *, label='')
dataclass
Bases: BaseLaw
A law that adds a Hermitian operator to the generator of a linear evolution.
The evolution is dx/dt = -i H x, where H is a Hermitian matrix on a tensor product of subsystems, such as the
Hamiltonian of quantum mechanics. Every generator law adds its operator to H.
Unlike DissipatorLaw, which adds an irreversible loss, it adds a reversible part that keeps the norm of the state.
Choose it when a state evolves by a unitary map, as a closed quantum system does.
It appears in quantum mechanics, where it gives the Schrödinger equation of spins and atoms, and in quantum walks on networks, which search a graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operator
|
object
|
The Hermitian operator, an expression of local operators of the namespaces the law receives. |
required |
label
|
str
|
The name of the law, or the empty string for a law without one. |
''
|
Examples:
>>> from sage.all__sagemath_symbolics import SR, var
>>> from skcomplex.systems.physics.common.quantum import GeneratorLaw
>>> from skcomplex.core.components.laws.utils import local_operator
>>> z = local_operator(SR.symbol('q'), SR.symbol('z'))
>>> GeneratorLaw(var('h') * z).operator
h*local_operator(q, z)
HilbertSpec(dimension, operators=dict(), basis=dict(), display='z')
dataclass
Bases: BaseSpec
A spec of the complex vector space of the states of an element.
It holds the local operators its laws build generators from. An element that takes a HilbertSpec and declares a
complex value, annotated with CC or CC^dimension, is a subsystem. Its laws read every local operator by
name, and a group of such elements is the tensor product of its members' spaces, in member order. The space names
QuantumCompiler, so a system of such elements compiles to a quantum problem by itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
int
|
The dimension of the space. |
required |
operators
|
Mapping[str, tuple[ArrayLike, bool]]
|
A mapping from operator name to its matrix and whether it is Hermitian, such as |
dict()
|
basis
|
Mapping[str, int]
|
A mapping from the label of a basis state to its index, such as |
dict()
|
display
|
str | None
|
The name of the operator whose expectation visuals show for a group of such elements, or |
'z'
|
Examples:
>>> import numpy as np
>>> from skcomplex.systems.physics.common.quantum import HilbertSpec
>>> spec = HilbertSpec(2, {'z': (np.diag([1.0, -1.0]), True)}, basis={'up': 0, 'down': 1})
>>> spec.basis['down']
1
JumpTrajectoriesSolver(atol=1e-12, rtol=1e-10, nsteps=100000, max_dimension=MAX_DIMENSION, *, random_state=None, until=math.inf)
Bases: OperatorSolver
A solver that samples the master equation by random jumps of a pure state.
It uses QuTiP's MCSolver. Every run is one trajectory, whose jumps are drawn from the simulation's random number
generator, so a seeded run is reproducible. A problem stored as a density matrix records the projector on the pure
state of every frame.
Choose it for large open quantum systems, where averaging pure states costs less than the density matrix. It appears in quantum optics, such as the photon counts of a cavity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atol
|
float
|
The absolute tolerance of the integrator. |
1e-12
|
rtol
|
float
|
The relative tolerance of the integrator. |
1e-10
|
nsteps
|
int
|
The largest number of internal steps between two frames. |
100000
|
Raises:
| Type | Description |
|---|---|
SolverError
|
The initial state is a mixed density matrix. |
Examples:
>>> import numpy as np
>>> from sage.all__sagemath_symbolics import SR
>>> from skcomplex.core.components import Observable
>>> from skcomplex.core.components.interactions import Interaction
>>> from skcomplex.core.components.laws.utils import local_operator
>>> from skcomplex.core.types import Role
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.physics.common.quantum import DissipatorLaw, JumpTrajectoriesSolver, QubitElement
>>> class Decay(Interaction):
... def __init__(self, label, *, target: Role = 'q', rate=1.0):
... super().__init__(label)
... self.target = target
... self.rate = rate
...
... def _prepare(self, s):
... super()._prepare(s)
... self.laws_ = [DissipatorLaw(local_operator(s.target.psi, SR.symbol('minus')), self.rate)]
>>> z = [Observable('z', element='q', variable='z')]
>>> solver = JumpTrajectoriesSolver()
>>> system = System(elements=[QubitElement('q')], interactions=[Decay('decay')], solver=solver).prepare(z)
>>> float(system.simulate(until=20.0, dt=1.0, random_state=0, observables=z).get_values('q.z')[-1])
-1.0
Source code in src/skcomplex/systems/physics/common/quantum/_operator.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
LindbladSolver(atol=1e-12, rtol=1e-10, nsteps=100000, max_dimension=MAX_DIMENSION, *, random_state=None, until=math.inf)
Bases: OperatorSolver
A solver of the Lindblad master equation with the MESolver of QuTiP.
Pure closed systems are solved with SESolver. It is the default solver of quantum systems. A system without
dissipators that starts in a pure state follows the linear evolution dx/dt = -i H x, which is the master equation
without dissipators.
Choose it for open quantum systems small enough to hold the density matrix. It appears in quantum optics and in the decoherence of qubits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atol
|
float
|
The absolute tolerance of the integrator. |
1e-12
|
rtol
|
float
|
The relative tolerance of the integrator. |
1e-10
|
nsteps
|
int
|
The largest number of internal steps between two frames. |
100000
|
Examples:
>>> import numpy as np
>>> from sage.all__sagemath_symbolics import SR
>>> from skcomplex.core.components import Observable
>>> from skcomplex.core.components.interactions import Interaction
>>> from skcomplex.core.components.laws.utils import local_operator
>>> from skcomplex.core.types import Role
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.physics.common.quantum import DissipatorLaw, LindbladSolver, QubitElement
>>> class Decay(Interaction):
... def __init__(self, label, *, target: Role = 'q', rate=1.0):
... super().__init__(label)
... self.target = target
... self.rate = rate
...
... def _prepare(self, s):
... super()._prepare(s)
... self.laws_ = [DissipatorLaw(local_operator(s.target.psi, SR.symbol('minus')), self.rate)]
>>> z = [Observable('z', element='q', variable='z')]
>>> decay = Decay('decay', rate=0.5)
>>> system = System(elements=[QubitElement('q')], interactions=[decay], solver=LindbladSolver()).prepare(z)
>>> values = system.simulate(until=2.0, dt=1.0, observables=z).get_values('q.z')
>>> bool(abs((1 + values[-1]) / 2 - np.exp(-1.0)) < 1e-6)
True
Source code in src/skcomplex/systems/physics/common/quantum/_operator.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
ModeElement(label, psi=0, *, hilbert=None)
Bases: Element
Bosonic mode of the Hilbert space it takes.
The mode has annihilate, create, and number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the mode. |
required |
psi
|
Annotated[object, CC]
|
The state in the Hilbert space: the number of quanta, or a vector. |
0
|
hilbert
|
HilbertSpec | None
|
The Hilbert space, |
None
|
Examples:
>>> from skcomplex.systems.physics.common.quantum import ModeElement, mode_space
>>> ModeElement('cavity', hilbert=mode_space(4)).hilbert.dimension
5
Source code in src/skcomplex/systems/physics/common/quantum/_mode.py
77 78 79 80 81 82 83 84 85 86 | |
OperatorMeasuresResult(problem, trajectory=None, energies=None, states=None, energy=None, state=None, purity=None, entropy=None)
dataclass
A result of operator_measures, a spectrum, state, or entanglement.
The purity or the entanglement is that of the states of its trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
OperatorProblemSpec
|
The compiled operator problem. |
required |
trajectory
|
Trajectory | None
|
The trajectory of |
None
|
energies
|
Array | None
|
The eigenvalues of the spectrum in increasing order, set for |
None
|
states
|
NDArray[complex128] | None
|
The eigenstates of the spectrum as columns over the whole space, set for |
None
|
energy
|
float | None
|
The ground state energy, set for |
None
|
state
|
NDArray[complex128] | None
|
The ground state vector, set for |
None
|
purity
|
Array | None
|
The purity at every frame, set for |
None
|
entropy
|
Array | None
|
The entanglement entropy at every frame, set for |
None
|
OperatorSolver(atol=1e-12, rtol=1e-10, nsteps=100000, max_dimension=MAX_DIMENSION, *, random_state=None, until=math.inf)
Bases: BaseSolver[OperatorProblemSpec]
An abstract solver of operator problems, such as evolutions on a Hilbert space.
They use a stepwise QuTiP solver. The solver is created at the first frame and again whenever the parameters change, from the current state, so a parameter update or a schedule applies from the next frame on. Coefficients that depend on time stay functions of time, and the others are folded into constant matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atol
|
float
|
The absolute tolerance of the integrator. |
1e-12
|
rtol
|
float
|
The relative tolerance of the integrator. |
1e-10
|
nsteps
|
int
|
The largest number of internal steps between two frames. |
100000
|
max_dimension
|
int
|
The largest dimension of the Hilbert space the solver accepts, which compiling checks. |
MAX_DIMENSION
|
random_state
|
int | Generator | None
|
The seed or generator of stochastic solvers. |
None
|
until
|
float
|
The end time of the run the solver prepares for, infinite for a run that goes on until it is stopped. |
inf
|
Source code in src/skcomplex/systems/physics/common/quantum/_operator.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
QuantumCompiler(backend=None, solver=None, observables=(), max_dimension=DEFAULT_MAX_DIMENSION)
Bases: OperatorCompiler
A compiler of quantum systems into problems of the operator form.
It adds every GeneratorLaw to the Hamiltonian and every DissipatorLaw to the dissipators of the master
equation. A system runs with LindbladSolver unless it is given a solver. The Hilbert space of a subsystem names
this compiler, so a system of quantum elements compiles with it by itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
BaseBackend | None
|
The backend requested for the problem, which the operator form does not use. |
None
|
solver
|
object
|
The solver the problem runs on. A solver with a largest dimension of its own limits the Hilbert space. |
None
|
observables
|
Sequence[Observable]
|
The observables to compute at every frame, labelled by their path from the root. |
()
|
max_dimension
|
int
|
The largest dimension of the Hilbert space, for a solver without a largest dimension of its own. |
DEFAULT_MAX_DIMENSION
|
Examples:
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.physics.common.quantum import QuantumCompiler, QubitElement
>>> compiler = QuantumCompiler().prepare(System(elements=[QubitElement('q')])).run()
>>> compiler.problem_.dimensions, type(compiler.solver_).__name__
((2,), 'LindbladSolver')
Source code in src/skcomplex/core/engine/compile/passes/_operator_compiler.py
93 94 95 96 97 98 99 100 101 | |
QubitElement(label, psi='up', *, hilbert=QUBIT)
Bases: Element
Two-level system.
The system has the Pauli matrices x, y, z and the ladder operators plus and minus.
The state 'up', also 'excited', is index 0 with z = 1, and 'down', also 'ground', is index 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the qubit. |
required |
psi
|
Annotated[object, CC]
|
The state in the Hilbert space: |
'up'
|
hilbert
|
HilbertSpec
|
The Hilbert space of the qubit. |
QUBIT
|
Examples:
>>> from skcomplex.systems.physics.common.quantum import QubitElement
>>> sorted(QubitElement('q').hilbert.operators)
['minus', 'plus', 'x', 'y', 'z']
Source code in src/skcomplex/systems/physics/common/quantum/_qubit.py
55 56 57 58 59 60 61 62 63 64 | |
SectorSpec(operator, value)
dataclass
Bases: BaseSpec
A spec of a sector of an operator system.
It holds the basis states whose eigenvalue of a conserved diagonal operator equals a value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operator
|
Callable[[SimpleNamespace], object]
|
A function of the system namespace that returns the conserved operator. |
required |
value
|
float
|
The eigenvalue of the operator. |
required |
Examples:
>>> from skcomplex.systems.physics.common.quantum import SectorSpec
>>> SectorSpec(lambda s: s.n, 2.0).value
2.0
UnitarySolver(atol=1e-12, rtol=1e-10, nsteps=100000, max_dimension=MAX_DIMENSION, *, random_state=None, until=math.inf)
Bases: OperatorSolver
A solver of the linear evolution dx/dt = -i H x of a pure state.
It uses QuTiP's SESolver.
Choose it for closed quantum systems. It appears in quantum mechanics, such as spin chains and quantum walks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atol
|
float
|
The absolute tolerance of the integrator. |
1e-12
|
rtol
|
float
|
The relative tolerance of the integrator. |
1e-10
|
nsteps
|
int
|
The largest number of internal steps between two frames. |
100000
|
Examples:
>>> import numpy as np
>>> from sage.all__sagemath_symbolics import SR
>>> from skcomplex.core.components import Observable
>>> from skcomplex.core.components.interactions import Interaction
>>> from skcomplex.core.components.laws.utils import local_operator
>>> from skcomplex.core.types import Role
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.physics.common.quantum import GeneratorLaw, QubitElement, UnitarySolver
>>> class Flip(Interaction):
... def __init__(self, label, *, target: Role = 'q'):
... super().__init__(label)
... self.target = target
...
... def _prepare(self, s):
... super()._prepare(s)
... self.laws_ = [GeneratorLaw(local_operator(s.target.psi, SR.symbol('x')) / 2)]
>>> z = [Observable('z', element='q', variable='z')]
>>> system = System(elements=[QubitElement('q')], interactions=[Flip('flip')], solver=UnitarySolver())
>>> trajectory = system.prepare(z).simulate(until=np.pi, dt=np.pi, observables=z)
>>> float(round(trajectory.get_values('q.z')[-1], 6))
-1.0
Source code in src/skcomplex/systems/physics/common/quantum/_operator.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
mode_space(quanta=4)
Return the Hilbert space of a bosonic mode truncated at a number of quanta.
Its operators are annihilate, create, and number, and its basis state n holds n quanta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quanta
|
int
|
The largest number of quanta, so the dimension is |
4
|
Returns:
| Name | Type | Description |
|---|---|---|
space |
HilbertSpec
|
The Hilbert space. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
The number of quanta is not a positive integer. |
Examples:
>>> from skcomplex.systems.physics.common.quantum import mode_space
>>> mode_space(4).dimension
5
Source code in src/skcomplex/systems/physics/common/quantum/_mode.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
operator_measures(source, *, kind='spectrum', n_states=6, parameters=None, sector=None, subsystems=None)
Return a spectrum, ground state, purity, or entanglement of an operator system.
The purity and the entanglement are those of its trajectory.
The kinds read a system or a trajectory:
'spectrum': the lowest eigenvalues and eigenstates of the generator, by exact diagonalisation. QuTiP diagonalises small spaces densely and larger ones with a sparse eigensolver. A sector restricts the basis to the states with a given eigenvalue of a conserved operator that is diagonal in the product basis, such as the total magnetisation.'ground-state': the lowest eigenvalue and eigenstate of the generator.'purity': the purityTr(rho^2)of the state of a trajectory at every frame, 1 for pure states.'entanglement': the von Neumann entropy of the reduced state of some subsystems at every frame of a trajectory, in nats.
It appears in physics and chemistry. It finds the energy levels of a molecule, the ground state of a chain of spins, and the entanglement of two qubits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
DataSource
|
The operator system or its compiled problem for |
required |
kind
|
str
|
The quantity to compute, one of the kinds above. |
'spectrum'
|
n_states
|
int
|
The number of eigenvalues of the spectrum. |
6
|
parameters
|
Mapping[str, object] | None
|
Parameter values by dotted name that replace the initial ones. |
None
|
sector
|
SectorSpec | None
|
The sector of a conserved diagonal operator whose basis states the spectrum keeps, or |
None
|
subsystems
|
Sequence[str] | None
|
The keys of the subsystems to keep, such as |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
result |
OperatorMeasuresResult
|
The spectrum, the ground state, the purity, or the entanglement entropy. |
Raises:
| Type | Description |
|---|---|
AnalysisError
|
The system or the trajectory holds no operators, the sector operator is not diagonal or lacks the eigenvalue, or a subsystem is not part of the trajectory. |
ParameterError
|
A parameter name is not part of the system. |
Examples:
>>> import numpy as np
>>> from sage.all__sagemath_symbolics import SR
>>> from skcomplex.core.components.interactions import Interaction
>>> from skcomplex.core.components.laws.utils import local_operator
>>> from skcomplex.core.types import Role
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.physics.common.quantum import GeneratorLaw, QubitElement, operator_measures
>>> class Field(Interaction):
... def __init__(self, label, *, target: Role = 'q'):
... super().__init__(label)
... self.target = target
...
... def _prepare(self, s):
... super()._prepare(s)
... self.laws_ = [GeneratorLaw(local_operator(s.target.psi, SR.symbol('z')))]
>>> system = System(elements=[QubitElement('q')], interactions=[Field('field')])
>>> operator_measures(system, n_states=2).energies.round(6).tolist()
[-1.0, 1.0]
>>> singlet = np.array([0, 1, -1, 0]) / np.sqrt(2)
>>> pair = System(elements=[QubitElement('a'), QubitElement('b')]).prepare()
>>> trajectory = pair.simulate(until=1.0, dt=1.0, initial_state={'system.state': singlet})
>>> result = operator_measures(trajectory, kind='entanglement', subsystems=['a'])
>>> bool(np.allclose(result.entropy, np.log(2)))
True
Source code in src/skcomplex/systems/physics/common/quantum/_measures.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 | |