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

QuantumPlaybook

Guides

  • QuantumPlaybook

Tech Notes

  • Addition of Numbers
  • Baker-Hausdorff Lemma
  • Chiral Fermion Random Circuit
  • Clifford Operators: How to Test
  • Commutation Relations for Qubits
  • Entanglement Distillation
  • Generalized Pauli Operators: Implementation
  • Hatano-Nelson-Kitaev Model: Monte Carlo Simulation
  • Hatano-Nelson Model: Monte Carlo Simulation
  • Kitaev Chain
  • Kitaev Random Circuit
  • Λ-Matter in a Cavity
  • Measurement of Total Pauli Z
  • Non-Unitary Dynamics of Quantum States
  • Partial Trace: Physical Meaning
  • Quantum Dot Systems
  • A Quantum Playbook
  • Magnetic Exchange Coupling
  • Solovay-Kitaev Algorithm
  • Spin Code
  • Symmetry Effects On Quantum Master Equations
  • Transmon: Quantum Phase Model
  • Transposition as a Supermap
  • Variational Quantum Classifier: Iris
  • Variational Quantum Classifier: Parity
  • Variational Quantum Eigensolver: Transverse-Field Ising Model

Symbols

  • BlochEnergy
  • BlochFunction
  • QuantumPlaybookCheckUpdate
  • QuantumPlaybookEdition
  • QuantumPlaybookUpdate
  • TransmonEnergy
  • TransmonExponent
  • TransmonFunction
  • TransmonHamiltonian
Variational Quantum Classifier: Parity
Setup
Quantum Circuit Layer
Data
Optimization
This demonstration is a translation of the tutorial “
Variational classifier
” in PennyLane by Maria Schuld. It demonstrates variational quantum classifiers; that is, quantum circuits that can be trained from labelled data to classify new data samples.
In particular, we will see that the variational quantum classifier can reproduce the parity function
f:
n
{0,1}
→{1,-1}
, where
f(x)=1
if there are even number of ones in
x
and
-1
otherwise. This optimization example demonstrates how to encode binary inputs into the initial state of the variational circuit, which is simply a computational basis state.
BasisEmbedding
Encodes data into amplitudes of a quantum state
FindMinimum
Find the minimum of a function
Frequently used function in variational classification.
Make sure to load the Q3 package.
In[10]:=
Needs["QuantumMob`Q3`"]
Setup
The qubits to be modeled are S[
i
,$] for
i=1,…,n
.
In[5]:=
Let[Qubit,S]
In[9]:=
$n=4;​​$N=Power[2,$n];​​kk=Range[$n];​​SS=S[kk,$]
Out[12]=
{
S
1
,
S
2
,
S
3
,
S
4
}
Data
Load and preprocess the data.
In[17]:=
bits=Tuples[{0,1},$n];​​parity=2*Mod[Count[#,1]&/@bits,2]-1;​​data=AssociationThread[bitsparity];​​Normal[data]//TableForm
Out[20]//TableForm=
{0,0,0,0}-1
{0,0,0,1}1
{0,0,1,0}1
{0,0,1,1}-1
{0,1,0,0}1
{0,1,0,1}-1
{0,1,1,0}-1
{0,1,1,1}1
{1,0,0,0}1
{1,0,0,1}-1
{1,0,1,0}-1
{1,0,1,1}1
{1,1,0,0}-1
{1,1,0,1}1
{1,1,1,0}1
{1,1,1,1}-1
Embed the input data to quantum states.
In[47]:=
$in=BasisEmbedding[SS]/@Keys[data];​​$in=Matrix/@$in;​​MatrixForm/@$in
Out[49]=

1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
,
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
,
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
,
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
,
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
,
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
,
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
,
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
,
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
,
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
,
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
,
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
,
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
,
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
,
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
,
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1

Quantum Circuit Layer
The parameters to be optimized are a[
layer,i,j
] for
i=1,…,n
and
j=1,…,3
.
In[50]:=
Let[Real,a]
Define a quantum circuit layer.
In[51]:=
layer[aa_?MatrixQ,ss:{__?QubitQ}]:=QuantumCircuit[​​Table[EulerRotation[aa〚k〛,ss〚k〛],{k,kk}],​​Apply[Sequence,CNOT@@@Transpose@{ss,RotateLeft@ss}]​​]/;Length[aa]Length[ss]​​layer[aa_?MatrixQ]:=layer[aa,SS]​​layer[n_Integer]:=QuantumCircuit@@Table[layer[Array[a[k],{$n,3}]],{k,n}]
For example, the following quantum circuit contains 24 real parameters, with three parameters
a[k,i,1],a[k,i,2],a[k,i,3]
associated with each qubit
i
on layer
k
.
In[54]:=
ll=layer[2]
Out[54]=
Optimization

Using a single layer

In[55]:=
$L=1;
Pick a quantum circuit layer to apply on the input states.
In[56]:=
op=layer[$L]
Out[56]=
In[63]:=
mat=Normal@Matrix[op];
In[64]:=
Z1=Matrix[S[1,3],SS];
In[65]:=
w0=Flatten@RandomReal[{0,1}Pi,{$L,$n,3}]
Out[65]=
{1.41694,0.769011,1.86193,2.64199,2.90684,1.28007,2.76019,2.58545,2.36391,1.93902,3.01061,1.32815}
The parameters to use.
In[66]:=
aa=a[Range@$L,Range@$n,{1,2,3}];
The parameters are angles and the following constraints are imposed.
In[68]:=
constraints=Thread[0≤aa<Pi];
In[72]:=
optimizer[ww_]:=Module[​​{ii=RandomChoice[Range@Length@data,4],​​yy,vv,avg,sol,cost},​​yy=Part[Values@data,ii];​​vv=Transpose@Part[$in,ii];​​vv=Transpose[mat.vv];​​avg=Map[Conjugate[#].Z1.#&,vv];​​cost=Mean@Abs[avg-yy]^2;​​EchoTiming[​​{cost,sol}=FindMinimum[{cost,constraints},Transpose@{aa,ww},​​StepMonitorPrintTemporary["Cost: ",cost],​​Method"IPOPT"];​​];​​Echo[{Chop[avg/.sol],yy,cost},"{avg, yy, cost}"];​​Return[aa/.sol]​​]
Test the optimizer.
Find the optimal values of the parameters iteratively.
From the optimized parameters, make predictions.

Verdict

◼
  • A single-layer is not sufficient to predict the parity values properly.
  • Using two layers without imposing the constraints

    Pick a quantum circuit layer to apply on the input states.
    Check which parameters are actually involved in the above quantum circuit.
    The set of parameters.
    We prepare the natural constraints, but in this example, we will not impose them.
    Test the optimizer.
    Find the optimal values of the parameters iteratively.
    From the optimized parameters, make predictions.

    Verdict

    ◼
  • Two layers is sufficient to properly predict the parity values.
  • Using two layers imposing the constraints

    Pick a quantum circuit layer to apply on the input states.
    Check which parameters are actually involved in the above quantum circuit.
    The set of parameters.
    Prepare the natural constraints for angles.
    Test the optimizer.
    Find the optimal values of the parameters iteratively.
    From the optimized parameters, make predictions.

    Verdict

    ◼
  • Two layers is sufficient to properly predict the parity values.
  • © 2025 Wolfram. All rights reserved.

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