Quantum Machine Learning

Mahn-Soo Choi (Korea University)

양자 기계 학습은 양자 알고리즘을 기계 학습 프로그램 내에 통합하는 것을 말한다. 가장 일반적인 형태는 양자 향상된 기계 학습으로, 고전 데이터 분석을 위한 기계 학습 알고리즘을 양자 컴퓨터에서 실행하게 된다. 기계 학습 알고리즘은 흔히 방대한 양의 데이터를 계산하는 데 사용되지만, 양자 기계 학습은 계산 속도와 데이터 저장 용량을 개선하기 위해 큐비트 및 양자 연산 또는 특수한 양자 시스템을 활용한다. 이는 고전 및 양자 처리를 모두 포함하는 하이브리드 방법을 포함한다. 이 발표에서는 울프램 언어를 이용하여 손쉽게 양자 기계 학습을 시뮬레이션하면서, 실제 양자 컴퓨터에서 이루어지는 양자 기계 학습의 원리를 공부하는 방법을 소개한다. 특히, 양자 시뮬레이션 프레임워크 Q3를 이용하여 몇 가지 기본적인 양자 기계 학습 예를 소개할 것이다.

Advertisements

Q3: Symbolic Quantum Simulation

({{, Q3 is a symbolic quantum simulation framework written in the Wolfram Language to help  study quantum information systems, quantum many-body systems, and quantum spin systems; hence the name Q3. It provides various tools and utilities for symbolic and numerical calculations  on these representative quantum systems.}})

Q3 is free. You can download it from the GitHub repository: https://github.com/quantum-mob/Q3 .

A Quantum Computation Workbook

({{, ({{Chapter 1. Postulates of Quantum Mechanics}, {Chapter 2. Quantum Computation: Overview }, {Chapter 3. Virtual Realization of Quantum Computers}, {Chapter 4. Quantum Algorithms}, {Chapter 5. Quantum Decoherence}, {Chapter 6. Quantum Error Correction Codes}, {Chapter 7. Quantum Information Theory}})}})

You can buy this book from the Springer site, https://link.springer.com/book/9783030912130.

School of Quantum

({{, 고려대학교(주관) 양자대학원은 고려대학교 등 9개 대학이 연합하여 양자기술 분야 박사급 전문인력 양성을 하기 위한 융합형 공유 대학원입니다. 본 양자대학원은 참여 대학, 연구소, 기업의 역량을 결집하여, 다양한 배경을 가진 인재들이 양자기술 분야에 성공적으로 진입하고, 전문적인 경력을 충실히 쌓으며 성장할 수 있게 함으로써, 국내 양자기술 연구 및 산업의 발전, 국가 경쟁력 확보, 미래 양자산업 발전을 이끌 수 있는 환경을 조성하여 제공하고자 합니다.}})

더 자세한 사항은 홈페이지(https://QuantumWorkforce.kr)를 참조하세요.

Quantum Circuits

Select a symbol to refer to a quantum register of qubits.

Let[Qubit,S]

Here is a typical quantum circuit including the CNOT and Hadamard gates as quantum circuit elements.

gate=QuantumCircuit[
CNOT[S[1],S[2]],
CNOT[S[1],S[3]],
S[{1,2,3},6]
]

Evaluate the above quantum circuit in terms of Pauli operators.

op=Elaborate[gate];
PauliForm[op]

Evaluate the matrix representation of the above quantum circuit.

Matrix[gate]//MatrixForm

In the above example, the quantum circuit involves only gate operations. More complete quantum circuit has input state and measurement as follows.

qc=QuantumCircuit[
Ket[S@{1,2,3}->{0,1,1}],
gate,"Spacer",
Measurement[S[3,3]]
]

Now, the evaluating the quantum circuit leads to the output state. Note that because of the measurement, the output state changes randomly at every run of the quantum circuit.

Elaborate[qc]

Take the measurement comes running the quantum circuit many times.

data=Table[Elaborate[qc];Readout[S[3,3]],20]

EchoTiming[data=Table[Elaborate[qc];Readout[S[3,3]],100];]
Histogram[data,FrameLabel->{"outcome","counts"}]

Machine Learning

Figure 1. A training loop consisting of feedforward and backpropagation. Courtesy of O. Dreessen, Analogue Dialogue (March 2023).

Wolfram Language supports various tools for machine learning. See the Machine Learning guide in Mathematica.

Quantum Machine Learning

Figure 2. Typical workflow of the quantum-classical hybrid method for quantum machine learning. Courtesy of Cerezo et al., Nature Review Physics (2021).

Figure 3. Comparison of classical deep neural network and variational quantum circuit. Courtesy of Y. Kwak et al., arXiv: 2202.11200.

Variational Quantum Classifier: Parity

Usually, Q3 is loaded automatically when Mathematica starts, but it does not harm to load it manually.

<<Q3`

Setup

The qubits to be modeled are S[i,$] for i=1,…,n.

Let[Qubit,S]

$n=4;
$N=Power[2,$n];
kk=Range[$n];
SS=S[kk,$]

Data

Load and preprocess the data.

bits=Tuples[{0,1},$n];
parity=2*Mod[Count[#,1]&/@bits,2]-1;
data=AssociationThread[bits->parity];
Normal[data]//TableForm

Embed the input data to quantum states.

$in=BasisEmbedding[SS]/@Keys[data];
$in=Matrix/@$in;
MatrixForm/@$in

Quantum Circuit Layer

The parameters to be optimized are a[layer,i,j] for i=1,…,n and j=1,…,3.

Let[Real,a]

Define a quantum circuit layer.

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.

ll=layer[2]

Optimization

Use two layers.

$L=2;

Pick a quantum circuit layer to apply on the input states.

op=layer[$L]

Check which parameters are actually involved in the above quantum circuit.

param=Cases[op,_a,Infinity]
Length[param]

mat=Normal@N@Matrix[op];
Dimensions[mat]

Z1=Matrix[S[1,3],SS];
Dimensions[Z1]

w0=Flatten@RandomReal[{0,1}Pi,{$L,$n,3}]
Length[w0]

The set of parameters.

aa=a[Range@$L,Range@$n,{1,2,3}];

We prepare the natural constraints, but in this example, we will not impose them.

constraints=Thread[0<=aa<Pi];

Clear[optimizer];
optimizer[ww_]:=Module[
{ii=RandomChoice[Range@Length@data,4],
yy,vv,avg,sol,cost},
yy=Part[Values@data,ii];
vv=Transpose@SparseArray@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"];
];
Return[aa/.sol]
]

Test the optimizer.

optimizer[w0]

Find the optimal values of the parameters iteratively.

$R=3;(* the number of iterations *)
ww=Nest[optimizer,w0,$R]

From the optimized parameters, make predictions.

mm=mat/.Thread[aa->ww];
vv=Transpose[mm.Transpose[$in]];
predicted=Map[Sign@Chop[Conjugate[#].Z1.#]&,vv]

predicted-Values[data]

Verdict

Two layers is sufficient to properly predict the parity values.

Using the Method->“IPOPT”, FindMinimum is significantly faster than the default method or any other documented methods (IPOTP seems to refer to the non-linear interior-point method from the IPOPT library).

Otherwise, FindMinimum is rather slow to handle 24 parameters.

Variational Quantum Classifier: Iris Dataset

Usually, Q3 is loaded automatically when Mathematica starts, but it does not harm to load it manually.

<<Q3`

Amplitude Embedding: Method

We want to embed classical data into a quantum state on quantum computer. One method is to use the amplitude embedding method.

Let[Qubit,S]

$n=2;
$N=Power[2,$n];
kk=Range[$n];
SS=S[kk,$]

Get the data.

aa=Normalize@RandomReal[{0,1},$N]

Embed the data into a quantum state.

in=AmplitudeEmbedding[aa,SS]

Embedding the data into a quantum state as above on actual quantum computer is not trivial. One implementation based on quantum circuit model may be achieved by the following function.

op=AmplitudeEmbeddingGate[aa,SS]

The above function may be represented by the following quantum circuit.

QuantumCircuit[Expand@op]

More explicitly, the above quantum circuit is equivalent to the following.

QuantumCircuit[ExpandAll@op]

The above quantum circuit may be further simplified.

qc=QuantumCircuit[GateFactor/@Expand@op]

Verify the above quantum circuit.

new=qc**Ket[SS]//Chop

Fidelity[new,in]

Data loading and embedding

Import the iris date and check the first three entries.

raw=Import[PacletObject["QuantumPlaybook"]["AssetLocation","Iris"],"Table"];
raw=Map[Most[#]->Round[Last@#]&,raw];
raw[[;;3]]

Renormalize the data.

data=Thread[Map[Normalize@Join[Take[#,2],0.3{1,0}]&,Keys@raw]->Values[raw]];
data[[1]]

Here, vector representation is nothing but the input data itself since we just simulate the amplitude embedding.

$in=Keys[data];

Features

gg=Map[Take[#,2]&,GroupBy[raw,Last,Map[First]],{2}];
Graphics[{PointSize[0.02],Red,Point/@gg[1],Blue,Point/@gg[-1]},
PlotLabel->"Original",
ImageSize->Medium,
Frame->True]

gg=Map[Take[#,2]&,GroupBy[data,Last,Map[First]],{2}];
Graphics[{PointSize[0.02],Red,Point/@gg[1],Blue,Point/@gg[-1]},
PlotLabel->"Renormalized",
ImageSize->Medium,
Frame->True]

RR=AmplitudeEmbeddingGate[#,SS]&/@Keys[data][[;;,;;-2]];
RR=List@@@Map[GateFactor,Expand/@RR,{2}]/.{CNOT->Nothing};
angles=Map[RotationAngle,RR, {2}];

tf=Positive[Values@data]//Flatten;
aa=Pick[angles[[;;,;;2]],tf];
bb=Pick[angles[[;;,;;2]],Not/@tf];
Graphics[{PointSize[0.02],Red,Point/@aa,Blue,Point/@bb},
PlotLabel->"Rotation angles for amplitude embedding",
ImageSize->Medium,
Frame->True]

Quantum Circuit Layer

Construct the basic quantum circuit layer to use.

Let[Real,w]

layer[lyr_Integer]:=QuantumCircuit[
Table[EulerRotation[w[lyr,k,{1,2,3}],S[k,$]],{k,$n}],
CNOT[S[1],S[2]]
]
layer[ll:{__Integer}]:=Apply[QuantumCircuit,layer/@ll]

Check it by showing two layers.

layer[{1,2}]

Optimization using three layers

$L=3;
$layer=layer[Range@$L]
$mat=Normal@Matrix[$layer,SS];

ww=w[Range@$L,Range@$n,{1,2,3}];
constraints=Thread[0<=ww<Pi];

w0=Flatten@RandomReal[{0,1}*Pi,{$L,$n,3}];

Z1=Matrix[S[1,3],SS];

Clear[optimizer]
optimizer[w0_]:=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@{ww,w0},
StepMonitor:>PrintTemporary["Cost: ",cost],
Method->"IPOPT"];
];
Return[ww/.sol]
]

Test the optimizer.

optimizer[w0]

Find the optimal values of the parameters iteratively.

$R=5;(* the number of iterations *)
new=Nest[optimizer,w0,$R]

From the optimized parameters, make predictions.

mm=$mat/.Thread[ww->new];

vv=Transpose[mm.Transpose[$in]];
predicted=Map[Sign@Chop[Conjugate[#].Z1.#]&,vv]

predicted-Values[data]

Distance-Based Quantum Classifier: Iris Dataset

M. Schuld, M. Fingerhuth, and F. Petruccione, EPL 119, 60002 (2017).

We are given a training data set
	 𝒟={(x^1)->(y^1),(x^2)->(y^2),…,(x^M)->(y^M)} 
of inputs (x^k)∈(ℝ^N) (N=(2^n)) with their respective target labels (y^k)∈{0,1}.

We want to determine the label of a new input x using the threshold function
	(Subscript[y, eval])(x)=sign((Underoverscript[Σ, (m=1), M]){1-(1/(4M))((|x-(x^m)|)^2)}(y^m)).

Let[Qubit,S]

Here, only two features of two samples are considered for the purpose of demonstration. This data is after standardization and normalization.

training=Association[
{0,1}->0,(*Iris sample 33*)
{0.789,0.615}->1 (*Iris sample 85*)
];
test=Association[
{-0.549,0.836}->0, (*Iris sample 28*)
{0.053,0.999}->0 (*Iris sample 36*)
];

To use  a quantum algorithm, each data point x is encoded to n qubits as amplitude (amplitude embedding),
	(Ket[(Subscript[ψ, x])]):=(Underoverscript[Σ, (j=0), ((2^n)-1)])(Ket[j])(Subscript[x, j])

trainingEmbed=Expand/@(AmplitudeEmbeddingGate[#,{S[3]}]&)/@Keys[training]
testEmbed=Expand/@(AmplitudeEmbeddingGate[#,{S[3]}]&)/@Keys[test]

To use the threshold function discussed above, we prepare a quantum state involving ancillary qubit, index register, data register, and class register (from the top to bottom).

state=QuantumCircuit[
Ket[S@{1,2,3,4}],S[{1,2},6],
ControlledGate[S[1]->0,testEmbed[[1]],"Label"->"(Subscript[U, in])"],
ControlledGate[S@{1,2}->{1,0},trainingEmbed[[1]],"Label"->"(Subscript[U, 1])"],
ControlledGate[S@{1,2}->{1,1},trainingEmbed[[2]],"Label"->"(Subscript[U, 2])"],
CNOT[S[2],S[4]]
]

The resulting quantum state is given by
	(Ket[Ψ])=(1/(Sqrt[2M]))(Underoverscript[Σ, (m=1), M])(Ket[m])⊗((Ket[0])⊗(Ket[(Subscript[ψ, x])])+(Ket[1])⊗(Ket[(Subscript[ψ, (x^m)])]))⊗(Ket[(y^m)])

Now, it is time to infer the label of the input x.

qc=QuantumCircuit[state,"Separator",
S[1,6],Projector[Ket[S[1]->0]],
Measurement[S[4,3]],
Ket[S[1]->0]
]

Running the above quantum circuit, one gets one instance of output state.

Elaborate[qc]//KetChop

Run the above quantum circuit many times to get the probability distribution of the measurement outcome.

EchoTiming[data=Table[Elaborate[qc];Readout[S[4,3]],10]]

EchoTiming[data=Table[Elaborate[qc];Readout[S[4,3]],100];]
Counts[data]

One determines the more frequent outcome as the label of the input x.

References

There are so many materials on quantum machine learning. This notebook is mainly based on the following two references.

M. Schuld and F. Petruccione (2018), Supervised Learning with Quantum Computers (Springer).

J. Biamonte et al., Nature 549, 195 (2017), “Quantum Machine Learning.”

For experts in machine learning, this review article may be interesting.

K. A. Tychola et al., Electronics 12, 2379 (2023), “Quantum Machine Learning--An Overview.”