Introductory Tutorial¶
This tutorial will illustrate the basics of how to use |toqito⟩
. This
will cover how to instantiate and use the fundamental objects that
|toqito⟩
provides; namely quantum states, channels, and measurements.
This is a user guide for |toqito⟩
and is not meant to serve as an
introduction to quantum information. For introductory material on quantum
information, please consult “Quantum Information and Quantum Computation” by
Nielsen and Chuang or the freely available lecture notes “Introduction to
Quantum Computing”
by John Watrous.
More advanced tutorials can be found on the tutorials page.
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 here
The standard basis ket vectors given as \(|0\rangle\) and \(|1\rangle\) where
can be defined in |toqito⟩
as such
from toqito.states import basis
# |0>
basis(2, 0)
array([[1],
[0]])
from toqito.states import basis
# |1>
basis(2, 1)
array([[0],
[1]])
One may define one of the four Bell states written as
using |toqito⟩
as
import numpy as np
e_0, e_1 = basis(2, 0), basis(2, 1)
u_0 = 1/np.sqrt(2) * (np.kron(e_0, e_0) + np.kron(e_1, e_1))
u_0
array([[0.70710678],
[0. ],
[0. ],
[0.70710678]])
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 = basis(2, 0), basis(2, 1)
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
array([[0.5, 0. , 0. , 0.5],
[0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. ],
[0.5, 0. , 0. , 0.5]])
Alternatively, we may leverage the bell
function in |toqito⟩
to
generate all four Bell states defined as
in a more concise manner as
from toqito.states import bell
import numpy as np
bell(0)
array([[0.70710678],
[0. ],
[0. ],
[0.70710678]])
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 information. For instance, the GHZ state
is a well-known 3-qubit quantum state. We can invoke this using |toqito⟩
as
from toqito.states import ghz
ghz(2, 3)
array([[0.70710678],
[0. ],
[0. ],
[0. ],
[0. ],
[0. ],
[0. ],
[0.70710678]])
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
from toqito.states import ghz
import numpy as np
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}")
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.states import bell
from toqito.state_props import is_pure
rho = bell(0) @ bell(0).conj().T
is_pure(rho)
True
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.states import bell
from toqito.state_props import is_ppt
rho = bell(2) @ bell(2).conj().T
is_ppt(rho)
False
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 = rho - tile(i) @ tile(i).conj().T
rho = rho / 4
is_separable(rho)
False
Further properties that one can check via |toqito⟩
may be found on this page.
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.
from toqito.states import bell
from toqito.state_metrics import fidelity
import numpy as np
# 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)
np.float64(1.0)
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
\[\left( \text{Tr} \otimes \mathbb{I}_{\mathcal{Y}} \right) \left(X \otimes Y \right) = \text{Tr}(X)Y\]
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.
from toqito.channels import partial_trace
import numpy as np
test_input_mat = np.arange(1, 17).reshape(4, 4)
partial_trace(test_input_mat)
array([[ 7, 11],
[23, 27]])
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
from toqito.channels import partial_trace
import numpy as np
test_input_mat = np.arange(1, 17).reshape(4, 4)
partial_trace(test_input_mat, [0])
array([[12, 14],
[20, 22]])
Another often useful channel is the partial transpose. The partial transpose is defined as
\[\left( \text{T} \otimes \mathbb{I}_{\mathcal{Y}} \right) \left(X\right)\]
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.
from toqito.channels import partial_transpose
import numpy as np
test_input_mat = np.arange(1, 17).reshape(4, 4)
partial_transpose(test_input_mat)
array([[ 1, 5, 3, 7],
[ 2, 6, 4, 8],
[ 9, 13, 11, 15],
[10, 14, 12, 16]])
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
from toqito.channels import partial_transpose
import numpy as np
test_input_mat = np.arange(1, 17).reshape(4, 4)
partial_transpose(test_input_mat, [0])
array([[ 1, 2, 9, 10],
[ 5, 6, 13, 14],
[ 3, 4, 11, 12],
[ 7, 8, 15, 16]])
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.states import basis
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)
[[0.65 0. ]
[0. 0.35]]
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.states import basis
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)
[[0.5 0.2]
[0.2 0.5]]
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:
from toqito.channels import phase_damping
import numpy as np
# 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)
[[1. +0.j 0.4472136+0.j]
[0.4472136+0.j 1. +0.j]]
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:
from toqito.channels import amplitude_damping
import numpy as np
# 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)
[[0.65 +0.j 0.41833001+0.j]
[0.41833001+0.j 0.35 +0.j]]
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.
from toqito.channels import bitflip
import numpy as np
# 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)
[[0.75+0.j 0. +0.j]
[0. +0.j 0.25+0.j]]
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_channel()
is equivalent to abitflip()
channel\(P_1 = 0\), and \(P_2 = 0\),
pauli_channel()
is equivalent to a Phase Flip channel\(P_1 = 0\), and \(P_3 = 0\),
pauli_channel()
is 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.
from toqito.channels import pauli_channel
import numpy as np
# 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)
[[0.6+0.j 0. +0.j]
[0. +0.j 0.4+0.j]]
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.
from toqito.measurement_props import is_povm import numpy as np meas_1 = np.array([[1, 0], [0, 0]]) meas_2 = np.array([[0, 0], [0, 1]]) meas = [meas_1, meas_2] is_povm(meas)
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.
from toqito.measurement_props import is_povm from toqito.rand import random_povm import numpy as np dim, num_inputs, num_outputs = 2, 2, 2 measurements = random_povm(dim, num_inputs, num_outputs) is_povm([measurements[:, :, 0, 0], measurements[:, :, 0, 1]])
Alternatively, the following matrices do not constitute a POVM set.
from toqito.measurement_props import is_povm
import numpy as np
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)
False
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:
from toqito.states import basis
from toqito.measurement_ops import measure
import numpy as np
e_0, e_1 = basis(2, 0), basis(2, 1)
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
measure(proj_0, rho)
np.float64(0.3333333333333334)
Similarly, the probability of obtaining outcome \(1\) is given by
measure(proj_1, rho)
np.float64(0.6666666666666667)
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 [1] 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.states import trine
from toqito.measurements import pretty_good_measurement
states = trine()
probs = [1 / 3, 1 / 3, 1 / 3]
pgm = pretty_good_measurement(states, probs)
pgm
[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 [2].
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.states import trine
from toqito.measurements import pretty_bad_measurement
states = trine()
probs = [1 / 3, 1 / 3, 1 / 3]
pbm = pretty_bad_measurement(states, probs)
pbm
[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 ]])]
References¶
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.