Quantum Basis and Quantum State (19)
In order to use QuantumBasis, one gives dimension information as arguments, which will be interpreted as the computational basis. Alternatively, an association can be given with the basis name as the key and the corresponding basis elements as the values.
Define a 2D quantum basis (computational):
Given a basis of dimension n, the basis elements will be indexed by the key
with :
Use QuantumBasis[n,m] to define a basis for qudits of dimension (for which the overall dimension will be ). For example, define a 2–2–2 dimensional quantum basis (with three qubits):
Use QuantumBasis[{n1,n2,…,nm}] to define an -dimensional Hilbert space of qudits as a list. For example, define a 3×5 dimensional quantum basis (with two qudits):
A basis can also be defined as an association with the basis element names as keys and the corresponding vectors as values:
There are many 'named' bases built into the quantum framework, including "Computational", "PauliX", "PauliY", "PauliZ", "Fourier", "Identity", "Schwinger", "Pauli", "Dirac" and "Wigner" bases:
After a basis object has been defined, it is straightforward to construct quantum states and operators. A quantum state is represented by a QuantumState object and a quantum operator is represented by QuantumOperator.
A pure quantum state is represented as a vector for which the elements are amplitudes. The corresponding basis should be given in this format: QuantumState[arg1,arg2], where arg1 specifies amplitudes or the density matrix, and arg2 specifies the basis. With no basis specified, the default basis will be the computational basis, the dimension of which depends on the amplitude vector given in arg1.
Note that we use the big endian convention, such that qubits are labelled left-to-right, starting with one. For example, the decimal representation of (which means ⊗⊗3) is . Additionally, for the eigenvalues of Pauli-Z, we have:
We shall denote the eigenstate {1,0} by (which corresponds to +1 eigenvalue), and {0,1} by (corresponding to the eigenvalue -1)
Define a pure 2-dimensional quantum state (qubit) in the Pauli-X basis:
If the basis is not specified, the default is the computational basis of dimensions ( qubits):
If the vector has more than 2 elements, it is interpreted as an -qubit state, unless the dimension is specified. If fewer than amplitudes are specified, right-padding is applied to reach the 'ceiling':
Here is the same amplitude vector, but this time with the dimension specified:
Binary strings can be also used as inputs:
Many 'named' states are available through the framework:
Using associations, one can create a superposition of states, where the keys are lists of corresponding indexes and the values are amplitudes.
Create a superposition of 3 qubits (i.e., QuantumBasis[2,3]) as
:
A superposition can also be created by simply adding two quantum state objects. For example, the previous state can also be constructed as follows:
With a built-in basis specified, amplitudes correspond to the basis elements. For example, use the Bell basis:
A state can also be defined by inputting a density matrix:
For pure states, one can get the corresponding normalized state vector:
Define a generic Bloch vector:
Test to see if it is a mixed state:
Calculate its Von Neumann entropy:
Compute its purity:
Note that one can directly use a Bloch vector as an input:
Test to see if a matrix is positive semidefinite:
A matrix that is not positive semidefinite cannot be a density matrix in standard quantum mechanics (with some exceptional cases, such as ZX formalism). Here is the result when we attempt to define a state using such a matrix:
When a matrix is given as input, but no basis is given, the default basis will be computational:
Using , define a quantum state in a 2–4 dimensional basis (and note the number of qudits):
Define a quantum state in 8D Hilbert space (with one 8-dimensional qudit only):
One can also define a state in a given basis, and then transform it into a new basis. For example, transform
, the computational basis, into the Pauli-X basis {,}:
Return the amplitudes:
Note that the states are the same, but defined in different bases:
One can use QuantumTensorProduct to construct different states or operators. Create a tensor product of a + state with three qubits
:
Another way of defining
is to first define a basis and then assign amplitudes:
Quantum Operators (10)
Quantum operators can be defined by a matrix or by specifying eigenvalues with respect to a QuantumBasis. Additionally, there are many built-in named operators that can be used.
Define a Pauli-X operator:
Apply a Pauli-X operator to a symbolic state
:
Test to see if the application of the Pauli-X operator yields the correct state:
Apply the Hadamard operator
:
Test to see if the application of the Hadamard operator yields the correct state:
One can also compose operators. Here's a composition of two Hadamard operators and one Pauli-Z operator:
Check the relation
:
Multi-qubit operators can take specific orders.
For instance, first define the state +:
Then, apply a Pauli-X operator on the second qubit only (by defining an order for the operator):
Test the result:
For multi-qudit cases, one can define an order or construct the operator using QuantumTensorProduct. For example:
Generalize Pauli matrices to higher dimensions:
Convert to matrix form:
Generalize the Hadamard operator to more qubits:
Test that the Hadamard operator can be constructed as a tensor product :
One can define a "ControlledU" operator with specific target and control qudits:
Return the control and target qudits:
Get the action of the operator (T-controlled (1, 2)) on :
Note that "CT" is also a 'named' controlled operator in our framework:
One can create a new operator by performing some mathematical operations (e.g., exponential, fraction power, etc.) on a quantum operator:
Show that the result is the same as a rotation operator around x:
Get the fractional power of the NOT operator:
Quantum Circuits (8)
Trace out the second subsystem in a 2-qubit state:
A partial trace can also be applied to QuantumBasis:
There are several metrics by which one can measure entanglement between two qudits, such as concurrence, entanglement entropy, negativity, etc. The calculation of entanglement measure is represented by the QuantumEntanglementMonotone function.
Plotting various entanglement measure for the state
:
To know whether a state is entangled or separable without computing its measure, use QuantumEntangledQ.
Check whether subsystems 1 and 3 are entangled in the "W" state:
In quantum information, there exist notions of distance between quantum states, such as fidelity, trace distance, Bures angle, etc. One can use QuantumDistance to compute the distance between two quantum states with various metrics.
Measure the trace distance between a pure state and a mixed state:
One may create a list of QuantumOperator and/or QuantumMeasurementOperator objects to build a quantum circuit, which can be represented as a QuantumCircuitOperator object.
Construct a quantum circuit that includes a controlled Hadamard gate:
The wire labels can be customized:
Construct a Toffoli gate as a circuit:
Show that the circuit is the same as the Toffoli gate:
One can define more than one control and target qubit:
Define the unitary operator and controlled-u operator:
Return the control qubits:
Return the target qubits:
Define the control-0 qubits:
Define a combination of control-0 and control-1 qubits:
Measurement operators can also be added to a quantum circuit. For a single-qubit unitary operator with eigenvalues ±1, a measurement of can be implemented in the following circuit. (Note that here, Pauli-Y is considered a operator):
Applying the circuit operators to a quantum state results in a quantum measurement:
Calculate the state of the second qubit after the measurement by tracing over the first qubit:
The post-measurement states of the second qubit should be the same as the Pauli-Y eigenstates.
Quantum Protocols & Algorithms (59)
Superdense coding (5)
Alice wants to send Bob two classical bits: 00, 01, 10, or 11. She can do so by using a single qubit if her qubit and Bob's are initially prepared as Bell states:
Depending on Alice's intended message, she will perform the following operations on her qubit:
1. To send 00, she does nothing.
2. To send 01, she applies the X-gate.
3. To send 10, she applies the Z-gate.
4. To send 11, she applies the X-gate and then the Z-gate.
Such operations can be represented as circuits, each with a final state resulting from application of its respective gate(s) to the initial Bell state:
Next, Alice sends her qubit to Bob through a quantum channel. If Bob performs a Bell measurement on his qubits, he receives Alice’s message.
Alice's 'messaging' with Bob can be fully implemented in a quantum circuit using two ancillary qubits. In the circuit, the 1st qubit is Alice's, the 2nd one is Bob's, and the 3rd and the 4th are the ancillary qubits. (Note that Alice sends her qubit to Bob and Bob performs a measurement on qubits 1 and 2):
Define an initial state as , where is the code that Alice wants to send Bob, which is encoded in two ancillary qubits (qubits 3 and 4).
Run the state through the circuit and return the outcome probabilities:
For each case, Bob finds Alice's code with probability 1.
Quantum teleportation (4)
Quantum teleportation is the reverse of superdense coding. Here, one wants to teleport a generic unknown quantum bit. Suppose that Alice wants to send a qubit (qubit 1) to Bob. To implement a quantum circuit for teleporting a qubit, Alice and Bob share an entangled state (qubits 2 and 3). Qubits 1 and 2 represent Alice's system, while qubit 3 is Bob's. The goal is to transfer the state of Alice’s first qubit to Bob’s qubit.
Set up a circuit:
The state to be teleported is =α+β, where α and β are unknown amplitudes. The input state of the circuit is as follows:
Given the result of Alice’s measurement on the first and second qubits, get the post-measurement states:
Regardless of measurement results, the state of the third qubit is the same state as the original first qubit =α+β (with only a normalization difference). Trace out the first and second qubits and compare the reduced state of qubit 3 only:
Deutsch–Jozsa algorithm (3)
Alice wants to determine if a Boolean function , used by Bob, is constant for all values of or balanced (where is an -digit binary number between 0 and -1). Consider a balanced implementation of for n=2 as follows: , , , meaning ⊻.
Given n=4, show that XOR is balanced:
Consider the case where Alice stores her query in a 2-qubit system, denoted by qubit 1 and qubit 2. Bob will store the answer in the 3rd qubit (the answer qubit). After Bob performs the oracle, Alice measures her qubits. If she gets anything other than , then the oracle is a balanced function.
Implement the algorithm:
Get the action of the quantum circuit as a measurement of the initial state:
The measurement indicates that the oracle is a balanced function.
Bernstein–Vazirani algorithm (7)
For an oracle given a state
, there is a transformation →(-1)s.x with s being a secret string:
As an example, take the secret string as the fourth state of a 3-qubit system (
):
Define the circuit as a list of operators: Hadamard → Oracle → Hadamard:
Shown below is a specific example for two qubits (i.e., ) and a secret string 11 (i.e., the fourth state
):
The secret string is , and it is measured as the final state:
Let's go through another example with 3 qubits and a secret string
:
The section string is the fifth state (
), and it is measured as the final state:
Grover's search algorithm (8)
Grover's algorithm is a search algorithm for one marked entry that is an n-bit binary number.
In order to implement the algorithm first define the oracle gate as a unitary operator:
As an example, define the oracle for marking the state :
Check that the only element of the operator matrix, –1, is the same as the decimal number of the corresponding binary number:
Implement a 3-qubit Grover algorithm by first defining a Grover circuit using a marker state:
Define the initial state:
Return the final state (output of the Grover circuit on the initial state):
The probability is highest for the fifth state,
.
Search for another entry; for example, :
Search for another entry; for example, :
Quantum phase estimation (7)
The Quantum phase estimation (QPE) algorithm solves the problem of finding in the eigenvalue equation = where is a unitary operator. The inputs of the algorithm are qubits at the initial state
. The output is
.
Here, use QPE to estimate the value of . This is possible if we choose
, which is QantumOperator[{"P",1}], since 
, and so QPE will return 
. Thus, by measuring the final state, you can estimate the value of .
Provided below is a step-by-step example of the algorithm for :
First, construct the QPE circuit:
Initialize the input state:
Feed the input state to the circuit and extract π from the measurement statistic:
The distribution peaks at the state 001, which is equivalent to integer 1. So, by this approximation,
, or :
One can build a PiEstimationCircuit for any . Here, n=5:
Approximate π with six qubits ():
Shor's algorithm
Shor's algorithm is an algorithm that can factorize a large number using a quantum computation. Given an integer , Shor's algorithm can return its factors and such that . This task is difficult for a classical computer if is large. The algorithm involves three main steps: classical pre-processing, quantum order-finding, and classical post-processing.
1. Classical preprocessing
- If is even, return 2 and /2.
- If is odd, pick a random number .
- Compute the greatest common divisor (GCD) of a and N. If GCD(, )!=1, return a and /a.
- If GCD(, )= 1, proceed to the order-finding algorithm.
2. Quantum order-finding
- Initialize a 2n qubit register (for ) in the state .
- Apply Hadamard gates to the first qubits.
- Apply modular exponentiation gate
, where the subscript indicates the number of qubits.
- Apply the inverse quantum Fourier transform to the first qubits.
- Measure the first qubits in the computational basis.
3. Classical post-processing
- Let s be the integer representation of the measurement outcome. Compute the order r of axmod N by finding the solutions to j(/r) = s, where j is an integer.
- If is odd or choose a different a and repeat the process.
- Otherwise, at least one of or
is a nontrivial factor of .
The quantum circuit used for quantum order-finding can be schematically illustrated as the following (n =3). (Note that U4 is the modular exponent gate):
Currently, the specific gate implementation of the modular exponentiation for arbitrary and a is unknown. However, there are some specific cases that have been studied. In the following, consider the implementation of quantum order-finding for (, a) = (15, 7) that has been demonstrated in experiments. Although one needs 8 qubits to factor 15 in general, by choosing a specific a, it is possible to use only 7 qubits (3 register, 4 ancilla).
Prepare the quantum order-finding circuit for (, a) = (15, 7):
Initialize the state
:
Apply the Shor circuit to the initial state:
In decimal notation, the possible states are:
,
,
, and
. If one obtains
, one must repeat the protocol. Otherwise:
Outcome: |2〉 → j(8/r) = 2 (j, r) = (1, 4)
p=GCD[(74/2+1) ,15] = 5 and q= GCD[(74/2-1),15] = 3
Outcome: |4〉 → j(8/r) = 4 (j, r) = (1, 2)
p= GCD[(72/2+1),15] = 1 and q= GCD[(72/2-1),15] = 3
Outcome: |6〉 → j(8/r) = 6 (j, r) = (3, 4)
p=GCD[(74/2+1) ,15] = 5 and q= GCD[(74/2-1),15] = 3
Thus, with 0.75 probability, one can find at least one nontrivial factor of 15.
Quantum Key Distribution: BB84 scheme (11)
As an example of a quantum key distribution (QKD), consider the BB84 protocol. Alice generates random bits and for each, randomly chooses either a Z-basis or X-basis of measurement. Consider a sequence of 9 bits:
Depending on her choice of bit and base, she follows the following protocol for sending a qubit to Bob:
With Alice’s bits and bases having been generated randomly, she will send Bob these qubits:
Bob randomly generates a sequence of either Z-bases or X-bases of measurement:
Thus, Bob applies the following measurement operators on the qubits he recieved from Alice:
Post-measurement, it is the case that, whenever Bob’s measurement basis is not the same as Alice’s, he gets two random results:
Return one possible realization of Bob' s series of measurements (noting that some results will be consistent and other random, depending on Alice' s qubit and Bob's measurement):
Bob assigns the bit 0 whenever he gets or , and the bit 1 for or :
Finally, Alice and Bob publicly share with each other the measurement basis that they used (in other words, they conduct a classical communication). If the basis is same, they each keep the bit; if not, they each disregard the corresponding bit. (Note that each does so separately, and each one obtains a secret key, which will be the same as the other's):
Alice and Bob now share a secret key. Note that the key length is less than the number of qubits Alice sent to Bob (because Bob picks the measurement basis randomly, and he might not get the same outcome as Alice). Alice can keep sending Bob qubits until they reach a desired length. This can be implemented as follows:
For example, the key length is set to 5 bits below: