Note
Click here to download the full example code
Introductory Tutorial¶
This tutorial illustrates the basics of how to use |toqito⟩. This will covers how to
instantiate and use the fundamental objects that |toqito⟩ provides; namely
quantum states, channels, and measurements.
This is an introduction to the functionality in |toqito⟩ and is not meant to serve as an
introduction to quantum information. For more information, please consult the book 1 or the freely available lecture notes 2.
This tutorial assumes you have |toqito⟩ installed on your machine. If you
do not, please consult the installation instructions in Getting Started.
States¶
A quantum state is a density operator
where \(\mathcal{X}\) is a complex Euclidean space and where \(\text{D}(\cdot)\) represents the set of density matrices, that is, the set of matrices that are positive semidefinite with trace equal to \(1\).
Quantum States¶
A complete overview of the scope of quantum states can be found in the states module.
The standard basis ket vectors given as \(|0\rangle\) and \(|1\rangle\) where
can be defined in |toqito⟩ as such
from toqito.matrices import standard_basis
# mkdocs_gallery_thumbnail_path = 'figures/logo.png'
# |0>
standard_basis(2)[0]
Out:
To get the other ket
Out:
One may define one of the four Bell states written as
using |toqito⟩ as
import numpy as np
e_0, e_1 = standard_basis(2)
u_0 = 1 / np.sqrt(2) * (np.kron(e_0, e_0) + np.kron(e_1, e_1))
u_0
Out:
The corresponding density operator of \(u_0\) can be obtained from
In |toqito⟩, that can be obtained as
import numpy as np
e_0, e_1 = standard_basis(2)
u_0 = 1 / np.sqrt(2) * (np.kron(e_0, e_0) + np.kron(e_1, e_1))
rho_0 = u_0 @ u_0.conj().T
rho_0
Out:
Alternatively, we may leverage the bell function in |toqito⟩ to
generate all four Bell states defined as
in a more concise manner as
Out:
The Bell states constitute one such well-known class of quantum states. There are many other classes of states that are widely used in the field of quantum For instance, the GHZ state
is a well-known 3-qubit quantum state. We can invoke this using |toqito⟩ as
Out:
While the 3-qubit form of the GHZ state is arguably the most notable, it is possible to define a generalized GHZ state
This generalized state may be obtained in |toqito⟩ as well. For instance,
here is the GHZ state \(\mathbb{C}^{4^{\otimes 7}}\) as
import numpy as np
from toqito.states import ghz
dim = 4
num_parties = 7
coeffs = [1 / np.sqrt(30), 2 / np.sqrt(30), 3 / np.sqrt(30), 4 / np.sqrt(30)]
vec = ghz(dim, num_parties, coeffs)
for idx in np.nonzero(vec)[0]:
print(f"Index: {int(idx)}, Value: {vec[idx][0]:.8f}")
Out:
Index: 0, Value: 0.18257419
Index: 5461, Value: 0.36514837
Index: 10922, Value: 0.54772256
Index: 16383, Value: 0.73029674
Properties of Quantum States¶
Given a quantum state, it is often useful to be able to determine certain properties of the state.
For instance, we can check if a quantum state is pure, that is, if the density matrix that describes the state has rank 1.
Any one of the Bell states serve as an example of a pure state
from toqito.state_props import is_pure
from toqito.states import bell
rho = bell(0) @ bell(0).conj().T
is_pure(rho)
Out:
Another property that is useful is whether a given state is PPT (positive partial transpose), that is, whether the state remains positive after taking the partial transpose of the state.
For quantum states consisting of shared systems of either dimension \(2 \otimes 2\) or \(2 \otimes 3\), the notion of whether a state is PPT serves as a method to determine whether a given quantum state is entangled or separable.
As an example, any one of the Bell states constitute a canonical maximally entangled state over \(2 \otimes 2\) and therefore should not satisfy the PPT criterion.
from toqito.state_props import is_ppt
from toqito.states import bell
rho = bell(2) @ bell(2).conj().T
is_ppt(rho)
Out:
As we can see, the PPT criterion is False for an entangled state in
\(2 \otimes 2\).
Determining whether a quantum state is separable or entangled is often useful
but is, unfortunately, NP-hard. For a given density matrix represented by a
quantum state, we can use |toqito⟩ to run a number of separability tests
from the literature to determine if it is separable or entangled.
For instance, the following bound-entangled tile state is found to be entangled (i.e. not separable).
import numpy as np
from toqito.state_props import is_separable
from toqito.states import tile
rho = np.identity(9)
for i in range(5):
rho -= tile(i) @ tile(i).conj().T
rho /= 4
is_separable(rho)
Out:
Further properties that one can check via |toqito⟩ may be found in the
state properties module.
Distance Metrics for Quantum States¶
Given two quantum states, it is often useful to have some way in which to quantify how similar or different one state is from another.
One well known metric is the fidelity function defined for two quantum states. For two states \(\rho\) and \(\sigma\), one defines the fidelity between \(\rho\) and \(\sigma\) as
where \(|| \cdot ||_1\) denotes the trace norm.
The fidelity function yields a value between \(0\) and \(1\), with \(0\) representing the scenario where \(\rho\) and \(\sigma\) are as different as can be and where a value of \(1\) indicates a scenario where \(\rho\) and \(\sigma\) are identical.
Let us consider an example in |toqito⟩ where we wish to calculate the
fidelity function between quantum states that happen to be identical.
import numpy as np
from toqito.state_metrics import fidelity
from toqito.states import bell
# Define two identical density operators.
rho = bell(0) @ bell(0).conj().T
sigma = bell(0) @ bell(0).conj().T
# Calculate the fidelity between `rho` and `sigma`
np.around(fidelity(rho, sigma), decimals=2)
Out:
There are a number of other metrics one can compute on two density matrices
including the trace norm, trace distance. These and others are also available
in |toqito⟩. For a full list of distance metrics one can compute on
quantum states, consult the docs.
Channels¶
A quantum channel can be defined as a completely positive and trace preserving linear map.
More formally, let \(\mathcal{X}\) and \(\mathcal{Y}\) represent complex Euclidean spaces and let \(\text{L}(\cdot)\) represent the set of linear operators. Then a quantum channel, \(\Phi\) is defined as
such that \(\Phi\) is completely positive and trace preserving.
Quantum Channels¶
The partial trace operation is an often used in various applications of quantum information. The partial trace is defined as
where \(X \in \text{L}(\mathcal{X})\) and \(Y \in \text{L}(\mathcal{Y})\) are linear operators over complex Euclidean spaces \(\mathcal{X}\) and \(\mathcal{Y}\).
Consider the following matrix
Taking the partial trace over the second subsystem of \(X\) yields the following matrix
By default, the partial trace function in |toqito⟩ takes the trace of the second
subsystem.
import numpy as np
from toqito.matrix_ops import partial_trace
test_input_mat = np.arange(1, 17).reshape(4, 4)
partial_trace(test_input_mat)
Out:
By specifying the sys = [0] argument, we can perform the partial trace over the first
subsystem (instead of the default second subsystem as done above). Performing the partial
trace over the first subsystem yields the following matrix
import numpy as np
from toqito.matrix_ops import partial_trace
test_input_mat = np.arange(1, 17).reshape(4, 4)
partial_trace(test_input_mat, sys=[0])
Out:
Another often useful channel is the partial transpose. The partial transpose is defined as
where \(X \in \text{L}(\mathcal{X})\) is a linear operator over the complex Euclidean space \(\mathcal{X}\) and where \(\text{T}\) is the transpose mapping \(\text{T} \in \text{T}(\mathcal{X})\) defined as
for all \(X \in \text{L}(\mathcal{X})\).
Consider the following matrix
Performing the partial transpose on the matrix \(X\) over the second subsystem yields the following matrix
By default, in |toqito⟩, the partial transpose function performs the transposition on
the second subsystem as follows.
import numpy as np
from toqito.matrix_ops import partial_transpose
test_input_mat = np.arange(1, 17).reshape(4, 4)
partial_transpose(test_input_mat)
Out:
By specifying the sys = [0] argument, we can perform the partial transpose over the
first subsystem (instead of the default second subsystem as done above). Performing the partial
transpose over the first subsystem yields the following matrix
import numpy as np
from toqito.matrix_ops import partial_transpose
test_input_mat = np.arange(1, 17).reshape(4, 4)
partial_transpose(test_input_mat, sys=[0])
Out:
Applying Quantum Channels
Another important operation when working with quantum channels is applying them to quantum states. apply_channel in |toqito⟩ provides a convenient way to apply a quantum channel (represented by its Choi matrix) to a given quantum state.
Here, we illustrate how to apply two widely used channels – the depolarizing channel and the dephasing channel – using apply_channel.
Depolarizing Channel
The depolarizing channel replaces a state with the maximally mixed state with probability \(p\) and leaves it unchanged with probability \((1-p)\). Mathematically, it is defined as
where \(\mathbb{I}\) is the identity operator and \(d\) is the dimension of the Hilbert space. The example below applies the depolarizing channel with \(p=0.3\) to the computational basis state \(|0\rangle\).
import numpy as np
from toqito.channel_ops import apply_channel
from toqito.channels import depolarizing
# Create a quantum state |0⟩⟨0|.
rho = np.array([[1, 0], [0, 0]])
# Generate the depolarizing channel Choi matrix with noise probability p = 0.3.
choi = depolarizing(2, 0.3)
# Apply the depolarizing channel using apply_channel.
output_state = apply_channel(rho, choi)
print(output_state)
Out:
Dephasing Channel
The dephasing channel reduces the off-diagonal elements of a density matrix without changing the diagonal entries, thereby diminishing quantum coherence. It is commonly expressed as
where \(Z\) is the Pauli-Z operator and \(p\) represents the dephasing probability. The example below demonstrates how to apply the dephasing channel with \(p=0.4\) to the plus state \(|+\rangle = \frac{1}{\sqrt{2}}(|0\rangle + |1\rangle)\).
import numpy as np
from toqito.channel_ops import apply_channel
from toqito.channels import dephasing
# Create a quantum state |+⟩⟨+|.
rho = np.array([[0.5, 0.5], [0.5, 0.5]])
# Generate the dephasing channel Choi matrix with dephasing probability p = 0.4.
choi = dephasing(2, 0.4)
# Apply the dephasing channel using apply_channel.
output_state = apply_channel(rho, choi)
print(output_state)
Out:
Noisy Channels¶
Quantum noise channels model the interaction between quantum systems and their environment, resulting in decoherence and loss of quantum information. The |toqito⟩ library provides implementations of common noise models used in quantum information processing.
Phase Damping Channel
The phase damping channel models quantum decoherence where phase information is lost without any energy dissipation. It is characterized by a parameter \(\gamma\) representing the probability of phase decoherence.
The phase damping channel can be applied to a quantum state as follows:
import numpy as np
from toqito.channels import phase_damping
# Create a density matrix with coherence.
rho = np.array([[1, 0.5], [0.5, 1]])
# Apply phase damping with γ = 0.2.
result = phase_damping(rho, gamma=0.2)
print(result)
Out:
Note that the off-diagonal elements (coherences) are reduced by a factor of \(\sqrt{1-\gamma}\), while the diagonal elements (populations) remain unchanged.
Amplitude Damping Channel
The amplitude damping channel models energy dissipation from a quantum system to its environment, such as the spontaneous emission of a photon. It is parameterized by \(\gamma\), representing the probability of losing a quantum of energy.
Here's how to use the amplitude damping channel:
import numpy as np
from toqito.channels import amplitude_damping
# Create a quantum state.
rho = np.array([[0.5, 0.5], [0.5, 0.5]])
# Apply amplitude damping with γ = 0.3.
result = amplitude_damping(rho, gamma=0.3)
print(result)
Out:
Bit-Flip Channel
The bit-flip channel randomly flips the state of a qubit with probability \(p\), analogous to the classical bit-flip error in classical information theory.
import numpy as np
from toqito.channels import bitflip
# Create a quantum state |0⟩⟨0|.
rho = np.array([[1, 0], [0, 0]])
# Apply bit-flip with probability = 0.25.
result = bitflip(rho, prob=0.25)
print(result)
Out:
Observe that the result is a mixed state with 75% probability of being in state \(|0\rangle\) and 25% probability of being in state \(|1\rangle\), as expected for a bit flip error with probability \(p = 0.25\).
Pauli Channel
The Pauli channel is a quantum noise model that applies a probabilistic mixture of Pauli operators to a quantum state. It is defined by a probability vector \((p_0, \ldots, p_{4^q - 1})\), where \(q\) is the number of qubits, and \(P_i\) are the Pauli operators acting on the system.
For example, when \(q = 1\), the Pauli operators are: \(P_0 = I\), \(P_1 = X\), \(P_2 = Y\), and \(P_3 = Z\). For multiple qubits, these operators are extended as tensor products.
It is also worth noting that when
-
\(P_2 = 0\), and \(P_3 = 0\),
pauli_channelis equivalent to abitflipchannel -
\(P_1 = 0\), and \(P_2 = 0\),
pauli_channelis equivalent to a Phase Flip channel -
\(P_1 = 0\), and \(P_3 = 0\),
pauli_channelis equivalent to a Bit and Phase Flip channel
The Pauli channel can be used to apply noise to an input quantum state or generate a Choi matrix.
import numpy as np
from toqito.channels import pauli_channel
# Define probabilities for single-qubit Pauli operators.
probabilities = np.array([0.5, 0.2, 0.2, 0.1])
# Define an input density matrix.
rho = np.array([[1, 0], [0, 0]])
# Apply the Pauli channel.
_, result = pauli_channel(prob=probabilities, input_mat=rho)
print(result)
Out:
Here, the probabilities correspond to applying the identity (\(I\)), bit-flip (\(X\)), phase-flip (\(Z\)), and combined bit-phase flip (\(Y\)) operators.
Measurements¶
A measurement can be defined as a function
satisfying
where \(\Sigma\) represents a set of measurement outcomes and where \(\mu(a)\) represents the measurement operator associated with outcome \(a \in \Sigma\).
POVM¶
POVM (Positive Operator-Valued Measure) is a set of positive operators that sum up to the identity.
Consider the following matrices:
Our function expects this set of operators to be a POVM because it checks if the operators sum up to the identity, ensuring that the measurement outcomes are properly normalized.
import numpy as np
from toqito.measurement_props import is_povm
meas_1 = np.array([[1, 0], [0, 0]])
meas_2 = np.array([[0, 0], [0, 1]])
meas = [meas_1, meas_2]
is_povm(meas)
Out:
Random POVM¶
We may also use random_povm to randomly generate a POVM, and can verify that a
randomly generated set satisfies the criteria for being a POVM set.
import numpy as np
from toqito.measurement_props import is_povm
from toqito.rand import random_povm
dim, num_inputs, num_outputs = 2, 2, 2
measurements = random_povm(dim, num_inputs, num_outputs)
is_povm([measurements[:, :, 0, 0], measurements[:, :, 0, 1]])
Out:
Alternatively, the following matrices do not constitute a POVM set.
import numpy as np
from toqito.measurement_props import is_povm
non_meas_1 = np.array([[1, 2], [3, 4]])
non_meas_2 = np.array([[5, 6], [7, 8]])
non_meas = [non_meas_1, non_meas_2]
is_povm(non_meas)
Out:
Measurement Operators¶
Consider the following state:
where we define \(u u^* = \rho \in \text{D}(\mathcal{X})\) and \(e_0\) and \(e_1\) are the standard basis vectors.
The measurement operators are defined as shown below:
import numpy as np
from toqito.matrices import standard_basis
from toqito.measurement_ops import measure
e_0, e_1 = standard_basis(2)
u = (1 / np.sqrt(3)) * e_0 + (np.sqrt(2 / 3)) * e_1
rho = u @ u.conj().T
proj_0 = e_0 @ e_0.conj().T
proj_1 = e_1 @ e_1.conj().T
Then the probability of obtaining outcome \(0\) is given by
Out:
Similarly, the probability of obtaining outcome \(1\) is given by
Out:
Pretty Good Measurement¶
Consider "pretty good measurement" on the set of trine states.
The pretty good measurement (PGM), also known as the "square root measurement" is a set of POVMs \((G_1, \ldots, G_n)\) defined as
This measurement was initially defined in 3 and has found applications in quantum state discrimination tasks. While not always optimal, the PGM provides a reasonable measurement strategy that can be computed efficiently.
For example, consider the following trine states:
from toqito.measurements import pretty_good_measurement
from toqito.states import trine
states = trine()
probs = [1 / 3, 1 / 3, 1 / 3]
pgm = pretty_good_measurement(states, probs)
pgm
Out:
[array([[0.66666667, 0. ],
[0. , 0. ]]), array([[0.16666667, 0.28867513],
[0.28867513, 0.5 ]]), array([[ 0.16666667, -0.28867513],
[-0.28867513, 0.5 ]])]
Pretty Bad Measurement¶
Similarly, we can consider so-called "pretty bad measurement" (PBM) on the set of trine states 4.
The pretty bad measurement (PBM) is a set of POVMs \((B_1, \ldots, B_n)\) defined as
Like the PGM, the PBM provides a measurement strategy for quantum state discrimination, but with different properties that can be useful in certain contexts.
from toqito.measurements import pretty_bad_measurement
from toqito.states import trine
states = trine()
probs = [1 / 3, 1 / 3, 1 / 3]
pbm = pretty_bad_measurement(states, probs)
pbm
Out:
[array([[0.16666667, 0. ],
[0. , 0.5 ]]), array([[ 0.41666667, -0.14433757],
[-0.14433757, 0.25 ]]), array([[0.41666667, 0.14433757],
[0.14433757, 0.25 ]])]
Total running time of the script: ( 0 minutes 0.463 seconds)
Download Python source code: intro_tutorial.py
Download Jupyter notebook: intro_tutorial.ipynb
Gallery generated by mkdocs-gallery
-
Michael A. Nielsen and Isaac L. Chuang. Quantum Computation and Quantum Information: 10th Anniversary Edition. Cambridge University Press, USA, 10th edition, 2011. ISBN 1107002176. ↩
-
John Watrous. The Theory of Quantum Information. Cambridge University Press, 2018. URL: https://cs.uwaterloo.ca/~watrous/TQI/TQI.pdf, doi:10.1017/9781316848142. ↩
-
Lane P Hughston, Richard Jozsa, and William K Wootters. A complete classification of quantum ensembles having a given density matrix. Physics Letters A, 183(1):14–18, 1993. ↩
-
Caleb McIrvin, Ankith Mohan, and Jamie Sikora. The pretty bad measurement. 2024. arXiv:2403.17252. ↩