# Wolfram Language Paclet Repository

Community-contributed installable additions to the Wolfram Language

Perform analytic and numeric quantum computations

Contributed by: Wolfram Research, Quantum Computation Framework team

The Wolfram Quantum Framework brings a broad, coherent design for quantum computation, together with a host of leading-edge capabilities and full integration into Mathematica and Wolfram Language. Starting from discrete quantum mechanics, the Framework provides a high-level symbolic representation of quantum bases, states and operators. The Framework can perform measurements and is equipped with various well-known states and operators, such as Bell states and Pauli operators. Using such simulation capabilities as a foundation, one can use the Framework to model and simulate quantum circuits and algorithms, and interoperate with many external quantum platforms from a Wolfram notebook.

All functions and objects in the Wolfram Quantum Framework seamlessly integrate with over 5,000 built-in functions that are accessible through Wolfram Language. The immediate availability of such functions allows one to study a full range of questions around quantum computation and serves as a valuable educational tool.

To install this paclet in your Wolfram Language environment,
evaluate this code:

`PacletInstall["Wolfram/QuantumFramework"]`

To load the code after installation, evaluate this code:

`Needs["Wolfram`QuantumFramework`"]`

This paclet works with Version 13.1 and higher of Wolfram Language.

Note that the big endian convention is in use, such that qubits are labeled left-to-right, starting with 1. For example, the decimal representation of (which means _{1}⊗_{2}⊗_{3}) is 2^{0}x+2^{1}y+2^{2}z.

Additionally, for the eigenvalues of Pauli-Z, there is:

In[1]:= |

Out[1]= |

Denote the eigenstate {1,0} by (which corresponds to +1 eigenvalue), and {0,1} by (which corresponds to the eigenvalue -1).

A computational basis can be defined by inputting the dimension information as arguments of QuantumBasis. Alternatively, an association can be used, with the basis name are given as the keys and the corresponding basis elements are specified as the values.

Define a 3D quantum basis (computational):

In[2]:= |

Out[2]= |

Given a basis of dimension n, the basis elements will be indexed by the key with :

In[3]:= |

Out[3]= |

With no input, the dimension is set as 2:

In[4]:= |

Out[4]= |

Use QuantumBasis[n,m] to define a basis for *m* qudits of dimension* n* (for which the overall dimension will be ). For example, define a 2×2×2-dimensional quantum basis:

In[5]:= |

Out[5]= |

Use QuantumBasis[{*n _{1},n_{2},…,n_{m}*}] to define an n

In[6]:= |

Out[6]= |

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

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

There are many 'named' bases built into the quantum framework, including "Computational", "PauliX", "PauliY", "PauliZ" ((or simply "X", "Y" or "Z"), "Fourier", "Identity", "Schwinger", "Pauli", "Dirac" and "Wigner":

In[9]:= |

Out[9]= |

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 state can be defined by two arguments: QuantumState[arg_{1},arg_{2}], where arg_{1} specifies amplitudes or the density matrix, and arg_{2} 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.

Define a pure 2D quantum state (qubit) in the Pauli-X basis:

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

If the basis is not specified, the default is the computational basis of dimensions ( qubits):

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

If the vector has more than 2 elements, it is interpreted as an -qubit state, unless the dimension is specified. If fewer than 2^{n} amplitudes are specified, right-padding is applied to reach the 2^{n} "ceiling":

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

Here is the same amplitude vector, but this time with the dimension specified:

In[16]:= |

Out[17]= |

Binary strings can also be used as inputs:

In[18]:= |

Out[18]= |

Many "named" states are available through the framework:

In[19]:= |

Out[19]= |

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

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |

A superposition can also be created by simply adding two quantum state objects. For example, the previous state can also be constructed as follows:

In[23]:= |

Out[24]= |

A state can also be defined by inputting a density matrix:

In[25]:= |

Out[25]= |

Define a generic Bloch vector:

In[26]:= |

In[27]:= |

Out[22]= |

Test to see if it is a mixed state:

In[28]:= |

Out[28]= |

Calculate its von Neumann entropy:

In[29]:= |

Out[29]= |

Compute its purity:

In[30]:= |

Out[30]= |

Note that one can directly use a Bloch vector as an input:

In[31]:= |

Out[31]= |

In[32]:= |

Out[32]= |

Test to see if a matrix is positive semidefinite:

In[33]:= |

Out[33]= |

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 it is attempted to define a state using such a matrix:

In[34]:= |

Out[34]= |

Note it is tagged as "Unknown state".

When a matrix is given as input but no basis is given, the default basis will be computational:

In[35]:= |

Out[22]= |

In[36]:= |

Out[36]= |

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

In[37]:= |

Out[37]= |

In[38]:= |

Out[38]= |

Define a quantum state in 8D Hilbert space (with one 8-dimensional qudit only):

In[39]:= |

Out[39]= |

In[40]:= |

Out[40]= |

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

In[41]:= |

Out[42]= |

Return the amplitudes:

In[43]:= |

Out[43]= |

In[44]:= |

Out[44]= |

Note that the states are the same, but defined in different bases:

In[45]:= |

Out[45]= |

One can use QuantumTensorProduct to construct different states or operators.

Create a tensor product of a + state with three qubits :

In[46]:= |

Return formula in Pauli-X basis:

In[47]:= |

Out[48]= |

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:

In[49]:= |

Out[49]= |

Apply a Pauli-X operator to a symbolic state :

In[50]:= |

Out[50]= |

Test to see if the application of the Pauli-X operator yields the correct state:

In[51]:= |

Out[51]= |

Apply the Hadamard operator :

In[52]:= |

Out[52]= |

Test to see if the application of the Hadamard operator yields the correct state:

In[53]:= |

Out[53]= |

One can also compose operators. Here is a composition of two Hadamard operators and one Pauli-Z operator:

In[54]:= |

Out[54]= |

Check the relation *σ*_{x}=H*σ*_{x}H:

In[55]:= |

Out[55]= |

Composite operators can be created in different ways:

In[56]:= |

Out[56]= |

Multi-qubit operators can take specific orders.

For instance, first define the state *α*+*β*:

In[57]:= |

Then, apply a Pauli-X operator on the second qubit only (by defining an order for the operator):

In[58]:= |

Out[58]= |

Test the result:

In[59]:= |

Out[59]= |

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

In[60]:= |

Out[60]= |

Generalize Pauli matrices to higher dimensions:

In[61]:= |

Out[61]= |

Convert to matrix form:

In[62]:= |

Out[62]= |

Hadamard operator to more qubits:

In[63]:= |

Out[63]= |

Test that the Hadamard operator can be constructed as a tensor product:

In[64]:= |

Out[64]= |

One can define a "Controlled" operator with specific target and control qudits:

In[65]:= |

Return the control and target qudits:

In[66]:= |

Out[66]= |

Get the action of the operator (*T*-controlled (1, 2)) on :

In[67]:= |

Out[67]= |

In[68]:= |

Out[68]= |

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

In[69]:= |

Out[69]= |

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

In[70]:= |

Out[70]= |

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

In[71]:= |

Out[71]= |

Get the fractional power of the NOT operator:

In[72]:= |

Out[72]= |

In[73]:= |

Out[73]= |

Time evolution can be implemented by adding a parametric specification to any operator. Then, using the EvolutionOperator property of a quantum operator, one can generate the corresponding time evolution operator.

Setting a Pauli-X operator as the Hamiltonian and evolving , we get:

In[74]:= |

Out[74]= |

Return the amplitudes at time :

In[75]:= |

Out[75]= |

Set up the Hamiltonian (as a time-dependent operator) in a magnetic field :

In[76]:= |

In[77]:= |

Out[77]= |

Get the state vector for the pure state:

In[78]:= |

Out[78]= |

In the Wolfram Quantum Framework, one can study projective measurements or, generally, any positive operator-valued measurement (POVM) using QuantumMeasurementOperator.

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

Measure a 3D system in its state basis:

In[79]:= |

Out[80]= |

In[81]:= |

Out[81]= |

Measure a 2-qubit system in the computational basis:

In[82]:= |

Out[83]= |

In[84]:= |

Out[84]= |

Note the labels for the corresponding eigenvalues, from 0 to n-1 as follows:

In[85]:= |

Out[85]= |

For composite systems, one can measure one or more qudits. This can be done by specifying an order for QuantumMeasurementOperator.

2D×3D composite system:

In[86]:= |

Measure only the first qudit:

In[87]:= |

Out[87]= |

Measure only the second qudit:

In[88]:= |

Out[88]= |

Measure both qudits:

In[89]:= |

Out[89]= |

One can also give a list of POVM elements by which to define the measurement operator:

In[90]:= |

Check that all POVM elements are explicitly positive semidefinite:

In[91]:= |

Out[91]= |

Check the completeness relations:

In[92]:= |

Out[92]= |

Measure POVMs on a quantum state:

In[93]:= |

Get the post-measurement states:

In[94]:= |

Out[94]= |

Get the corresponding probabilities:

In[95]:= |

Out[95]= |

Show that post-measurement states are the same as states initially defined as POVMs:

In[96]:= |

Out[96]= |

In the framework, there are some functionalities to explore the quantum distance, entanglement monotones and partial tracing, as well as other useful features.

Trace out the second subsystem in a 2-qubit state:

In[97]:= |

Out[97]= |

A partial trace can also be applied to QuantumBasis:

In[98]:= |

Out[98]= |

Given a Werner state (two qubits, with probability p), take the partial transpose with respect to qubit-2

In[99]:= |

Out[99]= |

In[100]:= |

Out[100]= |

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.

Plot various entanglement measures for the state :

In[101]:= |

Out[101]= |

To know whether a state is entangled (or part of that state), use QuantumEntangledQ.

Test if W-state of 4-qubits is entangled or not:

In[102]:= |

Out[103]= |

In[104]:= |

Out[104]= |

Test if qubit-1 and 3 are entangled or not:

In[105]:= |

Out[105]= |

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:

In[106]:= |

In[107]:= |

Out[107]= |

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:

In[108]:= |

Out[109]= |

The wire labels can be customized (for more info, please refer to Diagram documentation):

In[110]:= |

Out[110]= |

Construct a Toffoli gate as a circuit:

In[111]:= |

Out[111]= |

Show that the circuit is the same as the Toffoli gate:

In[112]:= |

Out[112]= |

Define a combination of control-0 and control-1 qubits:

In[113]:= |

Out[114]= |

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

In[115]:= |

Out[116]= |

Applying the circuit operators to a quantum state results in a quantum measurement:

In[117]:= |

Out[117]= |

Calculate the state of the second qubit after the measurement by tracing over the first qubit:

In[118]:= |

Out[118]= |

In[119]:= |

Out[119]= |

The post-measurement states of the second qubit should be the same as the Pauli-Y eigenstates.

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:

In[120]:= |

Out[121]= |

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.

Next, Alice sends her qubit to Bob. 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 first qubit is Alice's, the second one is Bob's and the third and the fourth are the ancillary qubits. Note that Alice sends her qubit to Bob and Bob performs a measurement on qubits 1 and 2:

In[122]:= |

Out[123]= |

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:

In[124]:= |

Out[124]= |

For each case, Bob finds Alice's code with probability 1.

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:

In[125]:= |

In[126]:= |

Out[126]= |

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

In[127]:= |

Given the result of Alice's measurement on the first and second qubits, get the post-measurement states:

In[128]:= |

Out[129]= |

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:

In[130]:= |

Out[130]= |

The goal of the Bernstein–Vazirani algorithm is to find a secret string bit as s using the action of a Bernstein–Vazirani oracle (i.e. which should be treated as a black box), which is defined by this transformation: → with the index register state of n-qubits, and the state of an ancillary qubit carrying the result.

A Bernstein–Vazirani oracle for the secret bit of 101:

In[131]:= |

Out[132]= |

A Bernstein–Vazirani circuit for the secret bit of 101:

In[133]:= |

Out[133]= |

As expected, the only measurement outcome corresponds to the secret bit string:

In[134]:= |

Out[134]= |

The goal of Grover's search algorithm is to find the solutions of a Boolean function f(x). This can be done using the named circuits or oracles in the quantum framework.

The action of a Boolean oracle is defined by this transformation: → with the index register state of n-qubits, and the state of an ancillary qubit carrying the result of the Boolean function f(x); meaning if =, then it will be if x is a solution of f(x), and unchanged if it is not.

Define a Boolean function of 3-SAT with five clauses:

In[135]:= |

Here is the truth table for this Boolean function:

In[136]:= |

Out[136]= |

The corresponding oracle's quantum circuit:

In[137]:= |

The diagram of the oracle:

In[138]:= |

Out[138]= |

Prepare the 4-qubit in the previous circuit (i.e. the ancillary qubit) as a 0-state, and then other qubits (1–3) in the index register states. To compare with the truth table, create the index register states |x> in the order |2^{n}-1> down to |0:

In[139]:= |

Create a list with elements {|x>,|q⊕f(x)}:

In[140]:= |

Out[140]= |

The action of a phase oracle can be defined as the following transformation: →(-1)^{f(x)} with |x〉the index register state of n-qubits:

In[141]:= |

The diagram of the oracle:

In[142]:= |

Out[142]= |

Create a list with elements {,(-1)^{f(x)}}:

In[143]:= |

Out[143]= |

Generate the corresponding Grover circuit using a Boolean oracle:

In[144]:= |

Out[144]= |

Generate a Grover circuit using the phase oracle for a Boolean function:

In[145]:= |

Out[145]= |

Given a Grover phase circuit for a Boolean function, calculate the probability of success of the algorithm:

In[146]:= |

In[147]:= |

Calculate the success probability after each iteration (note 111 is the solution of the Boolean function):

In[148]:= |

Plot the success probability:

In[149]:= |

Out[149]= |

As expected, this follows the formula for the probability of success , where M is the number of correct solutions out of a total N after k steps

The quantum phase estimation algorithm solves the problem of finding in the eigenvalue equation U*ψ*=ⅇ^{ⅈ2πθ}*ψ* , where is U a unitary operator. The inputs of the algorithm are qubits at the initial state ^{⊗n}, and the post-measurement state with largest probability is.

Consider a phase shift as the unitary operator in which the aim is to find the phase *θ*:

In[150]:= |

Out[150]= |

To specify the corresponding quantum circuit, one can use the built-in circuit "PhaseEstimation" that takes two input arguments: a unitary operator U and an integer n. The integer n specifies the number of qubits and controlled-U^{j} operators in the circuit, with j=0,1,…,n-1. The accuracy of phase estimation and the success probability depends on n:

In[151]:= |

In[152]:= |

Out[153]= |

Return the corresponding measurement, with all qubits prepared in state:

In[154]:= |

In[155]:= |

Out[155]= |

Given the outcome with the largest probability, estimate the phase:

In[156]:= |

Out[156]= |

As expected, it is a rough estimate, since a small value was chosen for n. If one increases n, with a higher probability, one can get a better estimate of the phase.

Estimate the phase (the expected value is 1/5=0.2) for n=6:

In[157]:= |

Out[157]= |

- WL built-in symbols
- Paclet dependencies
- Learn More »

Wolfram Language Version 13.1

- 1.4.2 – 28 June 2024
- 1.4.1 – 22 May 2024
- 1.4.0 – 15 May 2024
- 1.3.7 – 06 May 2024
- 1.3.6 – 24 April 2024
- 1.3.5 – 08 April 2024
- 1.3.4 – 13 March 2024
- 1.3.3 – 14 February 2024
- 1.3.2 – 03 February 2024
- 1.3.1 – 17 January 2024
- 1.3.0 – 14 January 2024
- 1.2.15 – 13 January 2024
- 1.2.14 – 25 December 2023
- 1.2.13 – 21 December 2023
- 1.2.12 – 21 November 2023
- 1.2.11 – 01 November 2023
- 1.2.10 – 19 October 2023
- 1.2.9 – 02 October 2023
- 1.2.8 – 16 September 2023
- 1.2.7 – 12 September 2023
- 1.2.6 – 18 August 2023
- 1.2.5 – 17 August 2023
- 1.2.4 – 03 August 2023
- 1.2.3 – 01 August 2023
- 1.2.2 – 21 July 2023
- 1.2.1 – 06 July 2023
- 1.2.0 – 28 June 2023
- 1.1.4 – 28 June 2023
- 1.1.3 – 14 June 2023
- 1.1.2 – 01 June 2023
- 1.1.1 – 23 May 2023
- 1.1.0 – 05 May 2023
- 1.0.35 – 03 May 2023
- 1.0.34 – 26 April 2023
- 1.0.33 – 19 April 2023
- 1.0.31 – 06 April 2023
- 1.0.30 – 03 April 2023
- 1.0.29 – 08 March 2023
- 1.0.28 – 27 January 2023
- 1.0.27 – 01 January 2023
- 1.0.26 – 03 December 2022
- 1.0.25 – 05 November 2022
- 1.0.24 – 18 October 2022
- 1.0.23 – 11 October 2022
- 1.0.22 – 04 October 2022
- 1.0.21 – 30 September 2022
- 1.0.19 – 15 August 2022
- 1.0.18 – 08 August 2022
- 1.0.17 – 16 July 2022
- 1.0.15 – 25 May 2022
- 1.0.14 – 10 May 2022
- 1.0.13 – 24 April 2022
- 1.0.12 – 05 April 2022
- 1.0.11 – 05 April 2022
- 1.0.10 – 05 April 2022
- 1.0.9 – 05 April 2022
- 1.0.8 – 04 April 2022
- 1.0.6 – 04 April 2022
- 1.0.2 – 18 January 2022
- 0.0.1 – 08 March 2023