Wolfram Research

# QuantumFramework

Perform analytic and numeric computation in Quantum Information Theory

Contributed By: Wolfram Research, Quantum Computation Framework team

Quantum Computing is an emerging technology that could revolutionize how computations are performed in the future. It operates based on the law of quantum mechanics - a radically different law of physics from the one our current computers are based on. A quantum computer may be able to solve problems that are difficult to solve using current computers such as breaking encryption, simulating a quantum system, or enhancing machine learning algorithms.

The Wolfram Language Quantum Computation Framework performs high-level analytic and numeric computations in Quantum Information Theory, allowing simulation of quantum circuits and quantum algorithms. Starting from discrete quantum mechanics, this framework can work with quantum states and quantum operators, implements measurements, performs basis manipulation, computes measure entanglement, and more. The semantics is very intuitive and it is equipped with various named states and operators such as Bell states, Pauli operators, universal quantum gates, and others. Thus, making it easy for people to simulate a quantum computer using the Wolfram Language.

## Details

This paclet works with V12.3 and more of Woflram Language Mathematica.

## Examples

### 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):

 In[1]:=
 Out[1]=

Given the basis dimension n, the basis elements will be indexed by ket with i=0,1,...,n-1

 In[2]:=
 Out[2]=

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):

 In[3]:=
 Out[3]=
 In[4]:=
 Out[4]=

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):

 In[5]:=
 Out[5]=
 In[6]:=
 Out[6]=

A basis can also be defined by an association with the basis element names as keys, and corresponding vectors as values.

 In[7]:=
 Out[7]=

There are many named-basis built in the quantum framework, {"Computational", "PauliX", "PauliY", "PauliZ","Fourier", "Identity","Schwinger", "Pauli", "Dirac", "Wigner"}:

 In[8]:=
 Out[8]=

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:

 In[9]:=
 Out[9]=
 In[10]:=
 Out[10]=

If the basis is not specified, the default is the computational basis of 2n dimension (n qubits):

 In[11]:=
 Out[11]=
 Out[12]=

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.

 In[13]:=
 Out[13]=
 In[14]:=
 Out[14]=

Same amplitude vector, but this time the dimension is specified:

 In[15]:=
 Out[16]=

Many named states are available for easy access:

 In[17]:=
 Out[17]=

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 :

 In[18]:=
 Out[18]=
 In[19]:=
 Out[19]=
 In[20]:=
 Out[20]=

A different way of creating superposition is simple adding two quantum state objects. For example, the previous state can be constructed as follows, too:

 In[21]:=
 Out[19]=

Once a built-in basis is specified, amplitudes correspond to the basis elements. For example, let's use Bell basis:

 In[22]:=
 Out[22]=
 In[23]:=
 Out[23]=
 In[24]:=
 Out[24]=

We can also define a state by inputting a density matrix:

 In[25]:=
 Out[25]=

For pure states, one can get the corresponding normalized state vector:

 In[26]:=
 Out[26]=

Define a generic Bloch vector:

 In[27]:=
 In[28]:=
 Out[20]=

Test if it is a mixed state:

 In[29]:=
 Out[29]=

Calculate Von Neumann Entropy:

 In[30]:=
 Out[30]=

Purity:

 In[31]:=
 Out[31]=

A pure state (by normalizing the vector r):

 In[32]:=
 Out[32]=

Purity:

 In[33]:=
 Out[33]=

Calculate Bloch Spherical Coordinates

 In[34]:=
 Out[34]=

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):

 In[35]:=
 Out[35]=

A non positive-semidefinite matrix as state:

 In[36]:=
 Out[36]=

For the matrices as an input, if no basis is given, the default basis will be computational.

 In[37]:=
 Out[34]=
 In[38]:=
 Out[38]=

Using ρ, define a quantum state in 2×4 dimensional basis (note the number of qudits):

 In[39]:=
 Out[39]=
 In[40]:=
 Out[40]=

Define a quantum state in 8D Hilbert space (one qudit, only) :

 In[41]:=
 Out[41]=
 In[42]:=
 Out[42]=

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 {,}:

 In[43]:=
 Out[36]=

Return amplitudes:

 In[44]:=
 Out[44]=
 Out[38]=

Note states are the same, only defined in different basis

 In[45]:=
 Out[45]=

One can use QuantumTensorProduct, to construct different states or operators. Create tensor product of plus state of three qubits :

 In[46]:=
 Out[36]=

Another way of defining is to first define a basis, and then assign amplitudes:

 In[47]:=
 In[48]:=
 Out[40]=
 In[49]:=
 Out[49]=

### 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:

 In[50]:=
 Out[50]=

Action of Pauli-x operator on a symbolic state :

 In[51]:=
 Out[51]=
 In[52]:=
 Out[52]=

 In[53]:=
 Out[53]=
 In[54]:=
 Out[54]=

One can also construct a composition of operators. For example, let's checking the relation :

 In[55]:=
 Out[55]=
 In[56]:=
 Out[56]=

Multi-qubit operators can take specific orders

Define state α+β:

 In[57]:=
 Out[56]=

Apply Pauli-X on the 2nd qubit only (by defining order in the operator)

 In[58]:=
 Out[58]=
 In[59]:=
 Out[59]=

For multi-qudit cases, one can define order, or construct the operator using QuantumTensorProduct. For example:

 In[60]:=
 Out[60]=

Generalization of Pauli matrices to higher dimensions:

 In[61]:=
 Out[61]=
 In[62]:=
 Out[62]=

Generalization of Hadamard to more qubits:

 In[63]:=
 Out[63]=
 In[64]:=
 Out[64]=

Action of Hadamard of 3-qubits on (a very common step in most quantum circuits):

 In[65]:=
 Out[65]=
 In[66]:=
 Out[66]=

One can define a “ControlledU” operator, with specific target and control qudits.

 In[67]:=

Return control and target qudits:

 In[68]:=
 Out[68]=

Action of T-controlled (1,2) on :

 In[69]:=
 Out[69]=
 In[70]:=
 Out[70]=

Note that "CT" is also a named controlled operator in our framework:

 In[71]:=
 Out[71]=

One can create new operator by doing some mathematical operations (e.g,. exponential, fractional power etc) on a quantum operator:

 In[72]:=
 Out[72]=

Show that the result is the same as rotation operator around x:

 In[73]:=
 Out[73]=

A fractional power of NOT operator:

 In[74]:=
 Out[74]=
 In[75]:=
 Out[75]=

### Describing Quantum Measurement (9)

In our quantum framework, one can study projective measurements or any general measurement (POVM), using QuantumMeasurementOperator.

#### PVM (projective measurements) (6)

A measurement can be defined by specifying the corresponding measurement basis.

Measurement of a one-qudit system in the state basis:

 In[76]:=
 Out[49]=
 In[77]:=
 Out[77]=

Post-measurement states per measurement result:

 In[78]:=
 Out[78]=

Measurement of a two-qudit system in the computational basis:

 In[79]:=
 Out[80]=
 In[81]:=
 Out[81]=

One can get the post-measurements states of measurement results:

 In[82]:=
 Out[82]=

For composite systems, one can specify measuring of one or more qudits. It can be done by specifying an order for QuantumMeasurementOperator. For example, measurement of a only 1st qudit of a two-qudit system in the state basis:

 In[83]:=
 Out[84]=
 In[85]:=
 Out[85]=
 In[86]:=
 Out[86]=

Check the post-measurement states are as and

 In[87]:=
 Out[87]=

One can use the following format for named basis and specifying corresponding eigenvalues: QuantumMeasurementOperator[name->eigenvalues]. For example, define measurement operator by a QuantumBasis object (as an eigenbasis) and a list of eigenvalues:

 In[88]:=
 Out[88]=

The representation of quantum state and operator can be changed according to any basis transformation. Basis transformation is applicable to QuantumState, QuantumOperator, and QuantumMeasurementOperator as demonstrated below:

Basis change for a quantum state:

 In[89]:=
 In[90]:=
 In[91]:=
 Out[91]=
 In[92]:=
 Out[92]=

Basis change for a quantum operator:

 In[93]:=
 In[94]:=
 In[95]:=
 Out[95]=
 In[96]:=
 Out[96]=

#### POVM (3)

One can also give a list of POVM elements to define measurement operator.

 In[97]:=

Check that all POVM elements are explicitly positive semi-definite:

 In[98]:=
 Out[98]=

Check the completeness relations:

 In[99]:=
 Out[99]=
 In[100]:=
 Out[100]=

Post-measurement states after measuring POVMs:

 In[101]:=
 Out[101]=

### 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:

 In[102]:=
 Out[102]=

Partial trace can also be applied to QuantumBasis:

 In[103]:=
 Out[103]=

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

 In[104]:=
 Out[104]=

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[105]:=
 Out[106]=
 In[107]:=
 Out[107]=

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:

 In[108]:=
 In[109]:=
 Out[109]=

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:

 In[110]:=
 Out[111]=

Decomposition of Toffoli gate:

 In[112]:=
 Out[112]=

Show above implementation is the same as Toffoli gate:

 In[113]:=
 Out[113]=

One can define more than one control and target qubits.

Define control and target qubits:
 In[114]:=
Define unitary operator and controlled-u operator:
 In[115]:=
 Out[116]=
Return the control qubits:
 In[117]:=
 Out[117]=
Return the target qubits:
 In[118]:=
 Out[118]=

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:

 In[119]:=
 Out[109]=

The result of circuit on a quantum state will be a quantum measurement:

 In[120]:=
 Out[120]=

Let us calculate state of 2nd qubit after the measurement, by tracing over 1st qudit.

 In[121]:=
 Out[121]=

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:

 In[122]:=
 Out[122]=
 In[123]:=
 Out[123]=

### 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:

 In[124]:=
 In[125]:=
 Out[125]=

The state to be teleported is =α+β, where α and β are unknown amplitudes. The input state of circuit is as follows:

 In[126]:=

Let’s look at post-measurement states, given the result of Alice’s measurement on 1st and 2nd qubits:

 In[127]:=
 Out[115]=

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:

 In[128]:=
 Out[128]=

#### 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:

 In[129]:=
 Out[129]=
 In[130]:=
 Out[130]=

Action of quantum circuit on the initial state :

 In[131]:=
 Out[131]=

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

 In[132]:=

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

 In[133]:=

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 )

 In[134]:=
 Out[134]=
 In[135]:=
 Out[135]=

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

 In[136]:=
 Out[136]=
 In[137]:=
 Out[137]=

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:
 In[138]:=
As an example, define oracle for marking state :
 In[139]:=
 Out[139]=

Check the only element of the operator matrix which is -1 is the same as the number of corresponding binary number.

 In[140]:=
 Out[141]=

We shall implement 3-qubit Grover algorithm.

Define Grover circuit using a marker state:
 In[142]:=
 In[143]:=
 Out[143]=
Define initial state:
 In[144]:=
Return the final state (output of Grover circuit on the initial state)
 In[145]:=
 Out[146]=

As one can see, the max probability is for the 5th state, .

Now let’s search for another entry, for example .

 In[147]:=
 Out[147]=

Search for another entry, for example .

 In[148]:=
 Out[148]=

#### 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:

 In[149]:=
 In[150]:=
 Out[150]=

Initialize the input state

 In[151]:=

Feed the input state to the circuit and extract π from the measurement statistic:

 In[152]:=
 In[153]:=
 Out[153]=

The distribution peaks at the state 001 which is equivalent to integer 1. So, we have 23-1/π≃1 or π ≃ 4.

 In[154]:=
 Out[154]=
 In[155]:=
 Out[155]=

Below is the code to build PiEstimationCircuit for any n:

 In[156]:=
 In[157]:=
 Out[157]=

Let's compute π with 6 qubits (n =5):

 In[158]:=
 Out[158]=
 In[159]:=
 Out[159]=
 In[160]:=
 Out[160]=

#### 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=|xn|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:

 In[161]:=
 Out[161]=

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):
 In[162]:=
 In[163]:=
 Out[163]=

Initialize the qubits state :

 In[164]:=
Apply the Shor circuit to the initial state:
 In[165]:=
 Out[165]=
 In[166]:=
 Out[166]=

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.

 In[167]:=
 In[168]:=
 Out[168]=

Define a initial state as , with xy the code Alice wants to send Bob:

 In[169]:=

As one can see, for any , Bob will get the same state with 100% probability:

 In[170]:=
 Out[170]=

#### 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:

 In[171]:=

Define the shared key:

 In[172]:=

Alice encodes her message by binary addition:

 In[173]:=
 Out[173]=

Bob decodes the message by binary addition:

 In[174]:=
 Out[174]=

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:

 In[175]:=
 Out[175]=
 In[176]:=
 Out[176]=

Alice encodes her bit key into a 10-qubits state:

 In[177]:=
 Out[178]=

Bob Also generate a random basis sequence:

 In[179]:=
 Out[179]=

Bob measures Alice's qubits in the chosen basis:

 In[180]:=
 Out[180]=

Let's suppose Bob obtains the following outcome:

 In[181]:=
 Out[181]=
 In[182]:=
 Out[182]=

After Bob obtains his result, Alice and Bob announce their basis choice publicly and decide which bits are to be discarded:

 In[183]:=
 Out[183]=

Alice's final key:

 In[184]:=
 Out[184]=

Bob's final key:

 In[185]:=
 Out[185]=

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.