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

\[\rho \in \text{D}(\mathcal{X})\]

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

\[| 0 \rangle = [1, 0]^{\text{T}} \quad \text{and} \quad | 1 \rangle = [0, 1]^{\text{T}}\]

can be defined in toqito as such

>>> from toqito.states import basis
>>> # |0>
>>> basis(2, 0)
array([[1],
       [0]])

>>> # |1>
>>> basis(2, 1)
array([[0],
       [1]])

One may define one of the four Bell states written as

\[u_0 = \frac{1}{\sqrt{2}} \left(| 00 \rangle + | 11 \rangle \right)\]

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

\[\begin{split}\rho_0 = u_0 u_0^* = \frac{1}{2} \begin{pmatrix} 1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 1 \end{pmatrix} \in \text{D}(\mathcal{X}).\end{split}\]

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

\[\begin{split}\begin{equation} \begin{aligned} u_0 = \frac{1}{\sqrt{2}} \left(| 00 \rangle + | 11 \rangle \right), &\quad u_1 = \frac{1}{\sqrt{2}} \left(| 00 \rangle - | 11 \rangle \right), \\ u_2 = \frac{1}{\sqrt{2}} \left(| 01 \rangle + | 10 \rangle \right), &\quad u_3 = \frac{1}{\sqrt{2}} \left(| 01 \rangle - | 10 \rangle \right), \end{aligned} \end{equation}\end{split}\]

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

\[| GHZ \rangle = \frac{1}{\sqrt{2}} \left( | 000 \rangle + | 111 \rangle \right)\]

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

\[| GHZ_n \rangle = \frac{1}{\sqrt{n}} \left( | 0 \rangle^{\otimes n} + | 1 \rangle^{\otimes n} \right).\]

This generalized state may be obtained in toqito as well. For instance, here is the GHZ state \(\mathbb{C}^{4^{\otimes 7}}\) as

\[\frac{1}{\sqrt{30}} \left(| 0000000 \rangle + 2| 1111111 \rangle + 3| 2222222 \rangle + 4| 3333333\rangle \right).\]

Using toqito, we can see this generates the appropriate generalized GHZ state.

>>> from toqito.states import ghz
>>> ghz(4, 7, np.array([1, 2, 3, 4]) / np.sqrt(30))
array([[0.18257419],
       [0.        ],
       [0.        ],
       ...,
       [0.        ],
       [0.        ],
       [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

\[|| \sqrt{\rho} \sqrt{\sigma} ||_1,\]

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

\[\Phi: \text{L}(\mathcal{X}) \rightarrow \text{L}(\mathcal{Y})\]

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

\[\begin{split}X = \begin{pmatrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \end{pmatrix}.\end{split}\]

Taking the partial trace over the second subsystem of \(X\) yields the following matrix

\[\begin{split}X_{pt, 2} = \begin{pmatrix} 7 & 11 \\ 23 & 27 \end{pmatrix}.\end{split}\]

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.array(
...     [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
... )
>>> 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

\[\begin{split}X_{pt, 1} = \begin{pmatrix} 12 & 14 \\ 20 & 22 \end{pmatrix}.\end{split}\]
>>> from toqito.channels import partial_trace
>>> import numpy as np
>>> test_input_mat = np.array(
...     [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
... )
>>> 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

\[\text{T}(X) = X^{\text{T}}\]

for all \(X \in \text{L}(\mathcal{X})\).

Consider the following matrix

\[\begin{split}X = \begin{pmatrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \end{pmatrix}.\end{split}\]

Performing the partial transpose on the matrix \(X\) over the second subsystem yields the following matrix

\[\begin{split}X_{pt, 2} = \begin{pmatrix} 1 & 5 & 3 & 7 \\ 2 & 6 & 4 & 8 \\ 9 & 13 & 11 & 15 \\ 10 & 14 & 12 & 16 \end{pmatrix}.\end{split}\]

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

\[\begin{split}X_{pt, 1} = \begin{pmatrix} 1 & 2 & 9 & 10 \\ 5 & 6 & 13 & 14 \\ 3 & 4 & 11 & 12 \\ 7 & 8 & 15 & 16 \end{pmatrix}.\end{split}\]
>>> from toqito.channels import partial_transpose
>>> import numpy as np
>>> test_input_mat = np.array(
...     [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
... )
>>> partial_transpose(test_input_mat, [0])
array([[ 1,  2,  9, 10],
       [ 5,  6, 13, 14],
       [ 3,  4, 11, 12],
       [ 7,  8, 15, 16]])

Measurements

A measurement can be defined as a function

\[\mu: \Sigma \rightarrow \text{Pos}(\mathcal{X})\]

satisfying

\[\sum_{a \in \Sigma} \mu(a) = \mathbb{I}_{\mathcal{X}}\]

where \(\Sigma\) represents a set of measurement outcomes and where \(\mu(a)\) represents the measurement operator associated with outcome \(a \in \Sigma\).