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

All functions and objects in the Wolfram Quantum Framework work seamlessly with the 5,000+ built-in made available through Wolfram Language. The immediate availability of such functions allows one to study a full range of questions around quantum computation, and can serve as a helpful resource for teaching.

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

`PacletInstall["Wolfram/QuantumFramework"]`

This paclet works with version 13.0 and higher of the Wolfram Language.

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

In[1]:= |

Out[1]= |

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

In[2]:= |

Out[2]= |

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

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

Use QuantumBasis[{*n _{1},n_{2},…,n_{m}*}] to define an -dimensional Hilbert space of qudits as a list. For example, define a 3×5 dimensional quantum basis (with two qudits):

In[5]:= |

Out[5]= |

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

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

There are many 'named' bases built into the quantum framework, including "Computational", "PauliX", "PauliY", "PauliZ", "Fourier", "Identity", "Schwinger", "Pauli", "Dirac" and "Wigner" bases:

In[8]:= |

Out[8]= |

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[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.

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:

In[9]:= |

Out[9]= |

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:

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 amplitudes are specified, right-padding is applied to reach the '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 be also 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]= |

With a built-in basis specified, amplitudes correspond to the basis elements. For example, use the Bell basis:

In[25]:= |

Out[25]= |

In[26]:= |

Out[26]= |

In[27]:= |

Out[27]= |

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

In[28]:= |

Out[28]= |

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

In[29]:= |

Out[29]= |

Define a generic Bloch vector:

In[30]:= |

In[31]:= |

Out[27]= |

Test to see if it is a mixed state:

In[32]:= |

Out[32]= |

Calculate its Von Neumann entropy:

In[33]:= |

Out[33]= |

Compute its purity:

In[34]:= |

Out[34]= |

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

In[35]:= |

Out[35]= |

In[36]:= |

Out[36]= |

Test to see if a matrix is positive semidefinite:

In[37]:= |

Out[37]= |

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:

In[38]:= |

Out[38]= |

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

In[39]:= |

Out[27]= |

In[40]:= |

Out[40]= |

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

In[41]:= |

Out[41]= |

In[42]:= |

Out[42]= |

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

In[43]:= |

Out[43]= |

In[44]:= |

Out[44]= |

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[45]:= |

Out[38]= |

Return the amplitudes:

In[46]:= |

Out[46]= |

In[47]:= |

Out[47]= |

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

In[48]:= |

Out[48]= |

One can use QuantumTensorProduct to construct different states or operators. Create a tensor product of a + state with three qubits :

In[49]:= |

Out[38]= |

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

In[50]:= |

In[51]:= |

Out[48]= |

In[52]:= |

Out[52]= |

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[53]:= |

Out[53]= |

Apply a Pauli-X operator to a symbolic state :

In[54]:= |

Out[54]= |

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

In[55]:= |

Out[55]= |

Apply the Hadamard operator :

In[56]:= |

Out[56]= |

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

In[57]:= |

Out[57]= |

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

In[58]:= |

Out[58]= |

Check the relation :

In[59]:= |

Out[59]= |

Multi-qubit operators can take specific orders.

For instance, first define the state +:

In[60]:= |

Out[59]= |

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

In[61]:= |

Out[61]= |

Test the result:

In[62]:= |

Out[62]= |

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

In[63]:= |

Out[63]= |

Generalize Pauli matrices to higher dimensions:

In[64]:= |

Out[64]= |

Convert to matrix form:

In[65]:= |

Out[65]= |

Generalize the Hadamard operator to more qubits:

In[66]:= |

Out[66]= |

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

In[67]:= |

Out[67]= |

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

In[68]:= |

Return the control and target qudits:

In[69]:= |

Out[69]= |

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

In[70]:= |

Out[70]= |

In[71]:= |

Out[71]= |

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

In[72]:= |

Out[72]= |

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

In[73]:= |

Out[73]= |

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

In[74]:= |

Out[74]= |

Get the fractional power of the NOT operator:

In[75]:= |

Out[75]= |

In[76]:= |

Out[76]= |

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[77]:= |

Out[74]= |

Return the amplitudes at time :

In[78]:= |

Out[78]= |

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

In[79]:= |

In[80]:= |

Out[78]= |

Get the state vector for the pure state:

In[81]:= |

Out[81]= |

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[82]:= |

Out[43]= |

In[83]:= |

Out[83]= |

Test to confirm that the measured states are the same as the basis states:

In[84]:= |

Out[84]= |

Measure a 2-qubit system in the computational basis:

In[85]:= |

Out[86]= |

In[87]:= |

Out[87]= |

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

In[88]:= |

Out[88]= |

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[89]:= |

Measure only the first qudit:

In[90]:= |

Out[90]= |

Measure only the 2nd qudit:

In[91]:= |

Out[91]= |

Measure both qudits:

In[92]:= |

Out[92]= |

One can use the following format for 'named' bases and their corresponding eigenvalues: QuantumMeasurementOperator[*name*→*eigenvalues*].

For example, define a measurement operator with a "named" QuantumBasis (as an eigenbasis) and a list of eigenvalues:

In[93]:= |

Out[93]= |

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

In[94]:= |

Check that all POVM elements are explicitly positive semidefinite:

In[95]:= |

Out[95]= |

Check the completeness relations:

In[96]:= |

Out[96]= |

Measure POVMs on a quantum state:

In[97]:= |

Get the post-measurement states:

In[98]:= |

Out[98]= |

Get the corresponding probabilities:

In[99]:= |

Out[99]= |

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

In[100]:= |

Out[100]= |

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

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

In[101]:= |

Out[101]= |

A partial trace can also be applied to QuantumBasis:

In[102]:= |

Out[102]= |

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 :

In[103]:= |

Out[103]= |

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[104]:= |

Out[102]= |

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[102]= |

The wire labels can be customized:

In[109]:= |

Out[109]= |

Construct a Toffoli gate as a circuit:

In[110]:= |

Out[110]= |

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

In[111]:= |

Out[111]= |

One can define more than one control and target qubit:

In[112]:= |

Define the unitary operator and controlled-*u* operator:

In[113]:= |

Out[84]= |

Return the control qubits:

In[114]:= |

Out[114]= |

Return the target qubits:

In[115]:= |

Out[115]= |

Define the control-0 qubits:

In[116]:= |

Out[44]= |

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

In[117]:= |

Out[118]= |

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[119]:= |

Out[111]= |

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

In[120]:= |

Out[120]= |

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

In[121]:= |

Out[121]= |

In[122]:= |

Out[122]= |

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[123]:= |

Out[124]= |

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:

In[125]:= |

Out[125]= |

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

In[126]:= |