Wolfram Language Paclet Repository

Community-contributed installable additions to the Wolfram Language

Primary Navigation

    • Cloud & Deployment
    • Core Language & Structure
    • Data Manipulation & Analysis
    • Engineering Data & Computation
    • External Interfaces & Connections
    • Financial Data & Computation
    • Geographic Data & Computation
    • Geometry
    • Graphs & Networks
    • Higher Mathematical Computation
    • Images
    • Knowledge Representation & Natural Language
    • Machine Learning
    • Notebook Documents & Presentation
    • Scientific and Medical Data & Computation
    • Social, Cultural & Linguistic Data
    • Strings & Text
    • Symbolic & Numeric Computation
    • System Operation & Setup
    • Time-Related Computation
    • User Interface Construction
    • Visualization & Graphics
    • Random Paclet
    • Alphabetical List
  • Using Paclets
    • Get Started
    • Download Definition Notebook
  • Learn More about Wolfram Language

QuantumFramework

Tutorials

  • Getting Started

Guides

  • Wolfram Quantum Computation Framework

Tech Notes

  • Bell's Theorem
  • Circuit Diagram
  • Example Repository Functions
  • Exploring Fundamentals of Quantum Theory
  • Quantum object abstraction
  • Tensor Network
  • Quantum Computation

Symbols

  • QuantumBasis
  • QuantumChannel
  • QuantumCircuitMultiwayGraph [EXPERIMENTAL]
  • QuantumCircuitOperator
  • QuantumDistance
  • QuantumEntangledQ
  • QuantumEntanglementMonotone
  • QuantumEvolve
  • QuantumMeasurement
  • QuantumMeasurementOperator
  • QuantumMeasurementSimulation
  • QuantumMPS [EXPERIMENTAL]
  • QuantumOperator
  • QuantumPartialTrace
  • QuantumPhaseSpaceTransform
  • QuantumShortcut [EXPERIMENTAL]
  • QuantumStateEstimate [EXPERIMENTAL]
  • QuantumState
  • QuantumTensorProduct
  • QuantumWignerMICTransform [EXPERIMENTAL]
  • QuantumWignerTransform [EXPERIMENTAL]
  • QuditBasis
  • QuditName
Tensor Network
QuantumCircuitOperator[...]["TensorNetwork"]
returns the tensor network of a circuit
TensorNetworkIndexGraph[...]
transforms a tensor network into a new graph with indices as vertices
TensorNetworkFreeIndices[...]
returns free indices in a tensor network index graph
ContractTensorNetwork[...]
contracts indices in a tensor network
Create a quantum circuit:
In[58]:=
circuit=
QuantumCircuitOperator
[{"S","H"2,"X"3,"CNOT","SWAP"{2,3},{1},{2},{3}}];​​circuit["Diagram"]
Out[59]=
Note in Wolfram quantum framework, each measurement result is saved in an ancillary quantum system (call it a detector, for example). So the true diagram will be something like this:
circuit["Diagram","ShowExtraQudits"True]
Note the first measurement is saved in the wire labeled as 0 and the rest goes backward, with negative labels.
Return the tensor network representation of the circuit:
net=circuit["TensorNetwork",GraphLayout{"LayeredDigraphEmbedding","Orientation"Left}]
The tensor network is a graph annotated with tensors and contraction indices.
GraphQ[net]&&TensorNetworkQ[net]
True
Lists all annotation keys available for the tensor network:
AnnotationKeys[{net,0}]
{Index,Tensor,VertexCoordinates,VertexLabels,VertexShape,VertexShapeFunction,VertexSize,VertexStyle}
Let’s look at tensor, vertex labels, and corresponding indexes in the tensor network:
list=Developer`FromPackedArray@VertexList[net];​​TableFormTranspose[Prepend[AnnotationValue[{net,list},#]&/@{"Tensor","Index"},Sort@AnnotationValue[net,VertexLabels]]],

VertexLabel
Tensor
Index
-20
SparseArray
Specified elements: 1
Dimensions: {2}

{
3
-2
}
-10
SparseArray
Specified elements: 1
Dimensions: {2}

{
2
-1
}
00
SparseArray
Specified elements: 1
Dimensions: {2}

{
1
0
}
1S
SparseArray
Specified elements: 2
Dimensions: {2,2}

{
1
1
,
1
1
}
2H
SparseArray
Specified elements: 4
Dimensions: {2,2}

{
2
2
,
2
2
}
3X
SparseArray
Specified elements: 2
Dimensions: {2,2}


3
3
,
3
3

4C⊗NOT
SparseArray
Specified elements: 4
Dimensions: {2,2,2,2}

{
1
4
,
2
4
,
4
1
,
4
2
}
5SWAP
SparseArray
Specified elements: 4
Dimensions: {2,2,2,2}


2
5
,
3
5
,
5
2
,
5
3

6
Measurement
1
SparseArray
Specified elements: 2
Dimensions: {2,2,2}

{
0
6
,
1
6
,
6
1
}
7
Measurement
2
SparseArray
Specified elements: 2
Dimensions: {2,2,2}

{
-1
7
,
2
7
,
7
2
}
8
Measurement
3
SparseArray
Specified elements: 2
Dimensions: {2,2,2}


-2
8
,
3
8
,
8
3

Note tensors in the tensor network are mixed type, meaning they consist of so-called “contravariant” (upper) indices and “covariant” (lower) indices. For example, the 2nd measurement (7th vertex), acts on qubit-2 (denoted by contravariant and covariant indices 2) and its result is saved on wire, denoted by the index “-1”.
Vertices correspond to circuit’s operators/gate indices, in addition to “Initial” tensor with index 0 (for the initial state):
VertexList[net]
{-2,-1,0,1,2,3,4,5,6,7,8}
Length@circuit["Flatten"]["Operators"]
8
%Max[%%]
True
Note that each edge represents (i.e., is tagged by) a contraction:
EdgeList[net]
0

1
0
,
1
1


1,-1

2
-1
,
2
2


2,-2

3
-2
,
3
3


3,1

1
1
,
4
1


4,2

2
2
,
4
2


4,4

2
4
,
5
2


5,3

3
3
,
5
3


5,4

1
4
,
6
1


6,5

2
5
,
7
2


7,5

3
5
,
8
3


8
Perform the contraction:
finalTensor=ContractTensorNetwork[net]
SparseArray
Specified elements: 2
Dimensions: {2,2,2,2,2,2}

Confirm that the result is the same as default circuit application:
circuit[]["Tensor"]finalTensor
True
Another tensor network representation uses indices as graph vertices with tensors as cliques:
indexNet=TensorNetworkIndexGraph[net,GraphLayout{"LayeredDigraphEmbedding","Orientation"Left}]
Note in above graph, the directed edges imply tensor contraction; also tensors are cliques in above graph
Free indices are the ones that left after contraction:
Free indices can be extracted as vertices with zero in- and out- degree:

Contraction and Einstein Summation

What ContractTensorNetwork does is in fact EinsteinSummation.
Show ContractTensorNetwork is the same as EinsteinSummation:
One can compare the performance, on how the relevant computation is done
Perform the contraction in the order of network’s EdgeList:
Optimize the order for contraction, using EinsteinSummation and symbolic tensors package:

Initial state different from ground state

Note that the initial tensor in the tensor network we studied here was a registered state. Additionally, one can start from any initial state.
Generate a random state:
Initialize the tensor network from above state:
See supplement info, for package-scoped symbols.
Show the tensor contraction is the same as transformation of state by the circuit:
Supplement info

Package-scoped symbols

FromTensorNetwork

Any directed graph can be turned into a tensor network, even if the graph is not annotated.

© 2025 Wolfram. All rights reserved.

  • Legal & Privacy Policy
  • Contact Us
  • WolframAlpha.com
  • WolframCloud.com