ABSTRACT (original book): This book provides a tutorial on Wolfram Language, the programming language powering the Mathematica computer algebra system. The main purpose of this book is to introduce the basic concepts required to start using Mathematica. The book contains lessons organized around various topics and use cases where the capabilities of Mathematica can be demonstrated. The lessons cover such elements of Wolfram Language as syntax, concepts used in linear algebra, graph theory, functions used to visualize data, and methods of solving equations. The introduced concepts are utilized to provide an overview of the basic elements of machine learning and quantum computing. CITATION (original article): Miszczak, J.A. (2024). Short Lessons on Wolfram Language Programming, Data Processing, Graphics, Machine Learning, and Quantum Computing with Mathematica. Quoqa Pubs. ISBN: 978-8366950016
Full Book: https://open.icm.edu.pl/handle/123456789/24302​
Amazon: https://a.co/d/8fhRcHL​
GitHub: https://github.com/jmiszczak/wl-lessons-examples
In this tutorial we introduce the basic block of used in quantum computing using only the basic data structures available in Wolfram language. We demonstrate how the concept from quantum computing can be mapped on the elements of Wolfram language, such as lists and functions.
The tutorial is based on the examples from the book: Jarosław A. Miszczak, Short Lessons on Wolfram Language: Programming, Data Processing, Graphics, Machine Learning, and Quantum Computing with Mathematica, ISBN: 978-83-66950-01-6, Quoqa Pubs, 2024. [Amazon.com] [Amazon.co.uk] [PDF at ICM OPEN] [More info].
This book is freely available from https://miszczak.eu/wl-lessons
More examples can be found at https://github.com/jmiszczak/wl-lessons-examples

Quantum bits as lists

In quantum computing we use qubits - quantum bits - instead of bits . Qubits are described by 2-dimensional vectors. Such vectors are called kets (not to be confused with cats), denotes by symbols such as
|ψ〉
or
|α〉
. This is so called Dirac notation, and it is used in physics textbooks.
To represent a qubit we use lists. Two special kets,
|0〉
and
|1〉
, are represented by base vectors.
In[]:=
Ket[0,2]:={1,0};​​Ket[1,2]:={0,1};
In[]:=
|0〉:=Ket[0,2];​​|1〉:=Ket[1,2];
As you can see, kets are just lists.
In[]:=
|0〉
Out[]=
{1,0}
In[]:=
|1〉
Out[]=
{0,1}
Note that sometimes we say that the state of a qubit is described by a vector, and sometime that a vector is a qubit.
In general, a qubit can be described by a linear combination of the base vectors.
In[]:=
Ket[{α,β}]:=α|0〉+β|1〉
Such a combination is called a superposition.
Quantum mechanics - and hence quantum computing - provides a probabilistic description of the system. For our qubits to behave properly in such world we nee to be sure that such vectors have length 1, or equivalently
2
α
+
2
β
=1.
Also, note that in Wolfram from version 13.3, Ket[{k}] (with one argument) is formatted as a ket in Dirac notation. This symbol has no predefined meaning in Wolfram language, but with the definition introduced above you can get a warning about the wrong number of arguments.

Using more qubit

For computing with more than just one qubit we need to introduce a Kronecker product. Wolfram provides a function KroncekerProduct, which can be used to calculate this product, so it is easy to see how it works. Let us define two matrices.
In[]:=
mA=Array[
a
#1,#2
&,{2,2}];​​mB=Array[
b
#1,#2
&,{2,2}];​​MatrixForm[mA]​​MatrixForm[mB]
Out[]//MatrixForm=
a
1,1
a
1,2
a
2,1
a
2,2
Out[]//MatrixForm=
b
1,1
b
1,2
b
2,1
b
2,2
Kronecker product of the above matrices is calculated as follows.
In[]:=
KroneckerProduct[mA,mB]//MatrixForm
Out[]//MatrixForm=
a
1,1
b
1,1
a
1,1
b
1,2
a
1,2
b
1,1
a
1,2
b
1,2
a
1,1
b
2,1
a
1,1
b
2,2
a
1,2
b
2,1
a
1,2
b
2,2
a
2,1
b
1,1
a
2,1
b
1,2
a
2,2
b
1,1
a
2,2
b
1,2
a
2,1
b
2,1
a
2,1
b
2,2
a
2,2
b
2,1
a
2,2
b
2,2
In quantum computing this product is denoted using ⊗ symbol, which can be input in Wolfram using ESC c * ESC shortcut. Symbol ⊗ is interpreted using CircleTimes function, but this function has no built-in meaning. To make our code more compact we define it meaning.
Kronecker product of two matrices is a matrix.
In[]:=
CircleTimes[x_?MatrixQ,y_?MatrixQ]:=KroneckerProduct[x,y];
Now we can write the Kronecker product using the from from the textbooks.
In[]:=
mA⊗mB//MatrixForm
Out[]//MatrixForm=
a
1,1
b
1,1
a
1,1
b
1,2
a
1,2
b
1,1
a
1,2
b
1,2
a
1,1
b
2,1
a
1,1
b
2,2
a
1,2
b
2,1
a
1,2
b
2,2
a
2,1
b
1,1
a
2,1
b
1,2
a
2,2
b
1,1
a
2,2
b
1,2
a
2,1
b
2,1
a
2,1
b
2,2
a
2,2
b
2,1
a
2,2
b
2,2
We also want to use Kronecker product with vectors, used to describe qubits. In this case, the result is a vector, so we use Flatten to get the right shape.
In[]:=
CircleTimes[x_?VectorQ,y_?VectorQ]:=Flatten[KroneckerProduct[x,y]];
Now we can join qubits to work with quantum registers.
In[]:=
Ket[0,2]⊗
Ket[0,2]+Ket[1,2]
Sqrt[2]
Out[]=

1
2
,

2
,0,0
In this case the firs qubit is in the base state
|0〉
, and the second one is in a superposition of states
|0〉
and 
|1〉
.
Finally, we also want to use this function with more qubits.
In[]:=
CircleTimes[a_,b__]:=CircleTimes[a,CircleTimes[b]]
So now we can have a register representing three qubits, in states
|0〉
,
|1〉
and
|0〉
.
In[]:=
Ket[0,2]⊗Ket[1,2]⊗Ket[0,2]
Out[]=
{0,0,1,0,0,0,0,0}
We can also use Dirac notation to achieve the same result.
In[]:=
|0〉⊗
|0〉+|1〉
√2
Out[]=

1
2
,

2
,0,0

Quantum logic gates and functions

Data processing in quantum computing is achieved via quantum gates. As in this tutorial we try to demonstrate how the concept from quantum computing can be mapped on the elements of Wolfram language, we use functions to introduce quantum gates and their matrix representation.
The simplest gates are based on the classical operations, such as the identity or negation. In such case the quantum operation, expressed as a matrix, is defined by the action of the function representing the gate on the base kets.
For example, if we have an identity operation, it can be express in Wolfram language as the function which keeps its input intact.
In[]:=
idFun=#&;
Now, if we apply this operation to the base kets, which can be done in Wolfram using Map, we get a corresponding matrix.
In[]:=
id=Map[idFun,{Ket[0,2],Ket[1,2]}];​​id//MatrixForm
Out[]//MatrixForm=

1
0
0
1

Indeed, in this case the corresponding matrix is the identity matrix.
Let us now try with the negation. The standard function for the negation flips between 0 and 1. Data in quantum computes are encoded in kets, so our function needs to flip between
|0〉
and
|1〉
.
In[]:=
notFun=Switch[#,​​Ket[0,2],Ket[1,2],​​Ket[1,2],Ket[0,2]​​]&;
Mapping such a function onto the base states we get the corresponding matrix.
In[]:=
not=Map[notFun[#]&,{Ket[0,2],Ket[1,2]}];​​not//MatrixForm
Out[]//MatrixForm=

0
1
1
0

A little more complicated example is provided by the conditional negation. In this case we only negate the second qubit if the first one is set to
|1〉
. This condition can be checking by checking if the first two bits are both set.
In[]:=
cnotFun=Switch[#[[1;;2]],{0,0},(id⊗not).#,_,#]&;
In this case we use Kronecker product to act on the input with the identity on the first qubit and with the not on the qubit.
In[]:=
cnot=Map[cnotFun[#]&,{Ket[0,2]⊗Ket[0,2],Ket[0,2]⊗Ket[1,2],Ket[1,2]⊗Ket[0,2],Ket[1,2]⊗Ket[1,2]}];​​cnot//MatrixForm
Out[]//MatrixForm=
1
0
0
0
0
1
0
0
0
0
0
1
0
0
1
0
This gate is frequently used in quantum algorithms as it can be used, along with gates acting on a single qubits, to express any quantum operation.

Reversibility of quantum gates

The above examples show that the quantum gate corresponding to a classical operation is represented by a matrix build from vectors obtained by the action of the classical gate on the base vectors.
But this condition is not sufficient. The resulting matrix has to preserve the length of the vector. We need to make sure that our vectors can be translated to probabilities after acting on them.
Let us take the following example.
In[]:=
invalidFun=Switch[#,_,Ket[0,2]]&;
In[]:=
invalid=Map[invalidFun[#]&,{Ket[0,2],Ket[1,2]}];
In[]:=
invalid//MatrixForm
Out[]//MatrixForm=

1
0
1
0

One can see, by employing Norm function, that this matrix does not preserve the length.
This function cannot be expressed as quantum gate because it is irreversible. Once we get the output it is impossible to recover the input arguments.

More quantum gates

The gates introduced above are just the quantum counterparts of the logical operations used in computer science. But quantum computers offer much more freedom when it comes to the data processing.
An operations such that its twofold application would be a not should fulfill the condition sqrtNot.sqrtNot is not.
In Wolfram we can calculate the matrix representation of this operation using MatrixPower function.
One can easily check that we get the matrix we wanted.
Indeed, any of the gates obtained in such a way would be a proper quantum logic gate.
Another example is based on the Fourier transform, which plays the prominent place in the Shor’s algorithm for factorization. In this case the quantum operation can be obtained in Wolfram by mapping Fourier function onto the base kets.
For one qubit this operation is know a the Hadamard gate.
This operation is often used in quantum algorithm to prepare the superposition of all possible inputs.
The quantum gate is a DFT matrix action on the input vector.

CITE THIS NOTEBOOK

Programming, data processing, graphics, machine learning, and quantum computing​
by Jaroslaw Miszczak​
Wolfram Community, STAFF PICKS, June 27, 2025
​https://community.wolfram.com/groups/-/m/t/3487075