Quantum Basis and State (19)
In our Quantum Framework, a quantum state is defined with respect to a QuantumBasis. For pure states, the given input can be a list of amplitudes, or an association with keys corresponds to basis elements and values as amplitudes.
Define a 2D quantum basis (computational):
Given the basis dimension n, the basis elements will be indexed by ket with i=0,1,...,n-1
Use a sequence as QuantumBasis[n,m], for n qudits of m-dimensional (overall dimension will be nm). For example, let us define a 2×2×2 dimensional quantum basis (three qubits):
Use a list as QuantumBasis[{n1,n2,...,nm}] for n1×n2×n2×...×nm dimensional Hilbert space of m qudits. For example, define a 3×5 dimensional quantum basis (two qudits):
A basis can also be defined by an association with the basis element names as keys, and corresponding vectors as values.
There are many named-basis built in the quantum framework, {"Computational", "PauliX", "PauliY", "PauliZ","Fourier", "Identity","Schwinger", "Pauli", "Dirac", "Wigner"}:
After a basis object has been defined, it is straightforward to use it to construct quantum states and operators. A quantum state is represented by QuantumState object and a quantum operator is represented by QuantumOperator.
For pure quantum states, a vector with elements as amplitudes, and the corresponding basis should be given in this format: QuantumState[amp_List,QuantumBasis[args]]. With no basis specified, the default basis will be the computational basis whose dimension depends on the amplitude vector.
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 2n dimension (n qubits):
If the vector has more than 2 elements, it is interpreted as a n-qubit state (by right-padding of zeros), unless the dimension is specified.
Same amplitude vector, but this time the dimension is specified:
Many named states are available for easy access:
Using associations, one can create a superposition of states, with keys as a list of corresponding indexes, and values as amplitudes. For example, let us create a superposition of 3 qubits (i.e., QuantumBasis[2,3]) as
:
A different way of creating superposition is simple adding two quantum state objects. For example, the previous state can be constructed as follows, too:
Once a built-in basis is specified, amplitudes correspond to the basis elements. For example, let's use Bell basis:
We can also define a state by inputting a density matrix:
For pure states, one can get the corresponding normalized state vector:
Define a generic Bloch vector:
Test if it is a mixed state:
Calculate Von Neumann Entropy:
Purity:
A pure state (by normalizing the vector r):
Purity:
Calculate Bloch Spherical Coordinates
A matrix that is not positive semi-definite (cannot be a density matrix in standard QM, but in ZX-formalism we can have situations like this):
A non positive-semidefinite matrix as state:
For the matrices as an input, if no basis is given, the default basis will be computational.
Using ρ, define a quantum state in 2×4 dimensional basis (note the number of qudits):
Define a quantum state in 8D Hilbert space (one qudit, only) :
One can also define a state in a given basis, and then transform it into a new basis. For example, let us transform in the computational basis into the basis of Pauli-X {,}:
Return amplitudes:
Note states are the same, only defined in different basis
One can use QuantumTensorProduct, to construct different states or operators. Create tensor product of plus state of three qubits :
Another way of defining is to first define a basis, and then assign amplitudes:
Quantum Operators (11)
Quantum operators can be define by a matrix, or specifying only eigenvalues with respect to a QuantumBasis. Additionally, there are many built-in named operators.
Define Pauli-x operator:
Action of Pauli-x operator on a symbolic state
:
Action of Hadamard operator
:
One can also construct a composition of operators. For example, let's checking the relation
:
Multi-qubit operators can take specific orders
Define state α+β:
Apply Pauli-X on the 2nd qubit only (by defining order in the operator)
For multi-qudit cases, one can define order, or construct the operator using QuantumTensorProduct. For example:
Generalization of Pauli matrices to higher dimensions:
Generalization of Hadamard to more qubits:
Action of Hadamard of 3-qubits on (a very common step in most quantum circuits):
One can define a “ControlledU” operator, with specific target and control qudits.
Return control and target qudits:
Action of T-controlled (1,2) on :
Note that "CT" is also a named controlled operator in our framework:
One can create new operator by doing some mathematical operations (e.g,. exponential, fractional power etc) on a quantum operator:
Show that the result is the same as rotation operator around x:
A fractional power of NOT operator:
Quantum Circuits (8)
In quantum information, it is often convenient to have a local state description of a multi-qubit state. For instance, Alice and Bob shared a (possibly entangled) quantum state. But, since they are really far away from each other, they do not have access to each other state. In such scenario, Alice's description of her part of quantum state is described by a "reduced state" that can be obtained by QuantumPartialTrace operation.
Trace out the second subsystem in a two-qubit state:
Partial trace can also be applied to QuantumBasis:
There are several metrics to measure entanglement between two qudits, such as concurrence, entanglement entropy, negativity etc. The calculation of entanglement measure is represented by QuantumEntanglementMonotone function.
Plotting various entanglement measure for the state 
To know whether a state is entangled or separable without computing its measure, QuantumEntangledQ is used.
Checking whether a subsystem 1 and 3 is entangled in "W" state:
In quantum information, there exist notions of distance between quantum state such as fidelity, trace distance, Bures angle, et cetera. One may compute distance between two quantum state using various metrics by QuantumDistance:
Measuring trace distance between a pure state and a mixed state:
One may create a list of QuantumOperator and/or QuantumMeasurementOperator to build a quantum circuit. A quantum circuit is represented by the symbol QuantumCircuitOperator, that can take single or multi qudit operators.
Example for the construction of controlled quantum gate:
Decomposition of Toffoli gate:
Show above implementation is the same as Toffoli gate:
One can define more than one control and target qubits.
Define control and target qubits:
Define unitary operator and controlled-u operator:
Return the control qubits:
Return the target qubits:
One can add measurement to a quantum circuit, too. For a single qubit unitary operator U with eigenvalues ±1, a measurement of U can be implemented in the following circuit. Note we have considered Pauli-Y as U operator:
The result of circuit on a quantum state will be a quantum measurement:
Let us calculate state of 2nd qubit after the measurement, by tracing over 1st qudit.
As said, the above quantum circuit should correspond to measurement of Pauli-Y, meaning that the post-measurement states of 2nd qubit should be the same as Pauli-Y eigenstates
Find eigenstates of Pauli-Y:
Quantum Protocols & Algorithms (47)
Quantum Teleportation (4)
We shall implement a quantum circuit for teleporting a qubit. The 1st and 2nd qubits represent Alice’s system, while the last one is Bob’s. The double lines carry classical bits:
The state to be teleported is =α+β, where α and β are unknown amplitudes. The input state of circuit is as follows:
Let’s look at post-measurement states, given the result of Alice’s measurement on 1st and 2nd qubits:
As one can see, regardless of measurement results, the state of 3rd qubit is the same state as the original 1st qubit =α+β (with only a normalization difference). Tracing out 1st and 2nd qubit, and compare the reduced state of only qubit 3:
Deutsch-Jozsa algorithm (2)
The main goal is to determine if a Boolean function f(x) is constant for all values of x or balanced, where x is a n-digit binary number between 0 and 2n-1, for example when n=2 we have state. We shall consider a balanced implementation of f(x) for n=2 as follows: f(0,0)=0, f(0,1)=f(1,0)=1, f(1,1)=0.
Implement the algorithm:
Action of quantum circuit on the initial state :
As one can see, measuring first two qubits give non-zero 11, indicating a balanced function.
Bernstein-Vazirani Algorithm (1)
For oracle, given a state we have the following transformation:→(-1)s.x, with s being a secret string
an example, lets take the secret string as the 4th state of a 3-qubit system ()
Define the circuit as a list of operators: Hadamards -> Oracle -> Hadamards
Let’s go through a specific example for 2 qubits (ie n=2) and a secret string 11 (ie the 4th state s=4, meaning )
As one can see, the secret string was and we got it as the final state.
Let's go through another example for 3 qubits and a secret string
As one can see, the section string was the 5th state () and we got it as the final state
Grover's Search Algorithm (5)
One can describe the Grover’s algorithm as the search for one marked entry which is an n-bit binary number. As the oracle, one can define the following unitary operator:
Define Oracle gate:
As an example, define oracle for marking state :
Check the only element of the operator matrix which is -1 is the same as the number of corresponding binary number.
We shall implement 3-qubit Grover algorithm.
Define Grover circuit using a marker state:
Define initial state:
Return the final state (output of Grover circuit on the initial state)
As one can see, the max probability is for the 5th state, .
Now let’s 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 U|ψ〉 = e2πiθ|ψ〉 where U is a unitary operator. The input and output of this algorithm are as follows:
INPUT: n+1 qubits at the initial state|ψ〉⊗|0〉⊗n
OUTPUT:
|ψ〉⊗|2nθ〉
Here, we will use QPE to estimate the value of π. This is possible if we choose
, which is QantumOperator[{"P",1}], since U|1〉=ei|1〉 and so QPE will return |1〉⊗|2n-1/π〉. Thus, by measuring the final state, we can estimate the value of π. Below, we will provide a step-by-step example of the algorithm for n = 3.
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, we have 23-1/π≃1 or π ≃ 4.
Below is the code to build PiEstimationCircuit for any n:
Let's compute π with 6 qubits (n =5):
Shor's Algorithm
Shor's algorithm is an algorithm that can factorize a big number using a quantum computer. Given an integer N, Shor's algorithm can return its factors p, q such that N = p × q. This task is difficult for a classical computer if N is large. The algorithm can be summarized in three main steps: classical pre-processing, quantum order-finding, and classical post-processing. Quantum computer is used only on the second step.
1. Classical preprocessing
- If N is even, return 2 and N/2.
- If N is odd, pick a random number 1<a<N
- Compute the greatest-common-divisor (GCD) of a and N. If GCD(a,N ) != 1, return a and N/a.
- If GCD(a, N) = 1, proceed to the order-finding algorithm.
2. Quantum order-finding
- Initialize 2n (for N = 2n-1) qubit register in state |0〉⊗(2n-1)⊗|1〉.
- Apply Hadamard gates H⊗n to the first n qubit.
- Apply modular exponentiation gate U|x〉n|y〉n=|x〉n|y⊕axmod N〉n where n subscript indicate the number of qubits.
- Apply inverse Quantum Fourier Transform to the first n qubits.
- Measure the first n qubits in 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(2n/r) = s where j is an integer.
- If r is odd or ar/2=-1mod N choose a different a and repeat the process.
- Otherwise, at least one of gcd(ar/2+1, N) or gcd(ar/2-1, N) is a non-trivial factor of N.
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 gates implementation of the modular exponentiation for arbitrary N and a is unknown. However, there are some specific cases that have been studied. Below, we will consider the implementation of quantum order-finding for (N, a) = (15, 7) that has been demonstrated in experiment. Although we need 8 qubits to factor 15 in general, by choosing specific a, it is possible to use only 7 qubits (3 register, 4 ancilla) as demonstrated below.
Quantum order-finding circuit for (N, a) = (15, 7):
Initialize the qubits state :
Apply the Shor circuit to the initial state:
In decimal notation, the possible states above are: |000〉, |010〉, |100〉, and |110〉. If we obtain |000〉, we need to 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, we can find at least one non-trivial factor of 15.
Superdense Coding (3)
In quantum teleportation we send a single qubit by sending two classical bits over a classical channel. Superdense coding is the reverse process. We send two classical bits using a single qubit and a quantum channel. First, Alice and Bob must sharesa maximally entangled state:
where subscripts A and B mean the qubit is held by Alice and Bob respectively. Then, depending on Alice intended message, she performs the following operations to her qubit:
1. If Alice wants to send 00, she does nothing to her qubit.
2. If Alice wants to send 01, she performs X-gate to her qubit.
3. If Alice wants to send 10, she performs Z-gate to her qubit.
4. If Alice wants to send 11, she performs X-gate and then Z-gate to her qubit.
After that, Alice sends her qubit to Bob through a quantum channel. Finally, if now Bob performs a Bell measurement on his qubits, he is guaranteed to receive Alice's message as demonstrated below.
Define a initial state as , with xy the code Alice wants to send Bob:
As one can see, for any , Bob will get the same state with 100% probability:
Quantum Cryptography (13)
Cryptography is the study of secure communications. One of the promises of quantum technology is to provide an inherently secure way to send information. The study of exploiting quantum mechanical properties for cryptographic tasks is known as quantum cryptography. One of the well-studied cases of quantum cryptographic protocols is secure quantum key distribution. Before we can understand this protocol, we need to understand a classical communication protocol known as one-time-pad.
In the one-time-pad scenario, Alice wants to send a secret bit string to Bob. To do this securely, they share a common bit key. Alice encodes her message by performing binary addition between her message and the key. She then sends the encoded message through a public channel to Bob. Bob can decode her message by performing the same binary addition. An eavesdropper can not know Alice's message because he does not have access to the key.
Define Alice's message:
Define the shared key:
Alice encodes her message by binary addition:
Bob decodes the message by binary addition:
So, using one-time-pad protocol, Alice and Bob can securely communicate. However, we have assumed that no one else has access to the bit key other than Alice and Bob. How can we generate a shared key between Alice and Bob securely? This is the problem of key distribution that can be solved by a quantum BB84 protocol as follows:
1. Alice generates a random bit key and a random basis sequence between Pauli-X and Pauli-Z bases.
2. If the basis is Pauli-Z, she encodes the corresponding bit key in the , basis. If the basis is Pauli-X, she encodes the corresponding bit key in the
basis. For example, if her bit keys are 0100 and the bases are XZZX then Alice’s encoding qubits are .
3. Alice sends these qubits to Bob through a quantum channel.
4. Bob also generates a random sequence of basis between Pauli-X and Pauli-Z bases. He then measures Alice's qubits with respect to that basis sequence and keeps the measurement outcome private.
5. Alice and Bob both announce their choice of bases. Then they discard the bits in which their corresponding bases do not match. The remaining bits of Alice and Bob are guaranteed to be perfectly correlated and thus Alice and Bob have shared a key.
Alice generates a bit key and a random basis sequence of length 5:
Alice encodes her bit key into a 10-qubits state:
Bob Also generate a random basis sequence:
Bob measures Alice's qubits in the chosen basis:
Let's suppose Bob obtains the following outcome:
After Bob obtains his result, Alice and Bob announce their basis choice publicly and decide which bits are to be discarded:
Alice's final key:
Bob's final key:
In this particular implementation, Alice and Bob successfully share a 4-bits key. They can repeat the procedure until the key has the same length as the length of the intended message. This protocol is secure because prior to the bases choice's public announcement, an eavesdropper can only extract the keys by guessing the measurement basis. However, by doing this he risks disturbing the state. So, to detect an eavesdropper, Alice and Bob can sacrifice some of their key bits to make sure that their bits perfectly correlate. If they do not correlate, this means Alice and Bob have detected the presence of an eavesdropper.