From 653004147cf78fbaa5779e7c148ccab779b2e497 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Fri, 11 Aug 2023 19:40:38 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- Pyfiles/circuits.py | 649 ++++++++++++++++++-------------------- Pyfiles/circuits_n1.py | 639 ++++++++++++++++++------------------- Pyfiles/experiments.py | 19 +- Pyfiles/optimizer.py | 10 +- Pyfiles/quantumcircuit.py | 66 ++-- 5 files changed, 643 insertions(+), 740 deletions(-) diff --git a/Pyfiles/circuits.py b/Pyfiles/circuits.py index a15bd1b..8859fcd 100644 --- a/Pyfiles/circuits.py +++ b/Pyfiles/circuits.py @@ -8,34 +8,32 @@ def circuit1(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is 8L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + if repeat!=0: qc.barrier(qr) - - for l in range(L): + for _ in range(L): for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + return qc @@ -44,40 +42,38 @@ def circuit2(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is 8L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 qc.cx(qr[3],qr[2]) qc.cx(qr[2],qr[1]) qc.cx(qr[1],qr[0]) - - - if repeat!=0: + + + if repeat!=0: qc.barrier(qr) - for l in range(L): - + for _ in range(L): qc.cx(qr[1],qr[0]) qc.cx(qr[2],qr[1]) qc.cx(qr[3],qr[2]) - + for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + return qc @@ -88,46 +84,44 @@ def circuit3(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (11)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[3],qr[2]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[2],qr[1]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[1],qr[0]) count=count+1 - if repeat!=0: + if repeat!=0: qc.barrier(qr) - - - for l in range(L): - + + + for _ in range(L): qc.crz(theta[count],qr[1],qr[0]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[2],qr[1]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[3],qr[2]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + return qc @@ -137,47 +131,45 @@ def circuit4(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (11)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - - count=0 - for l in range(L): + count=0 + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[3],qr[2]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[2],qr[1]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[1],qr[0]) count=count+1 - if repeat!=0: + if repeat!=0: qc.barrier(qr) - - - for l in range(L): - + + + for _ in range(L): qc.crx(theta[count],qr[1],qr[0]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[2],qr[1]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[3],qr[2]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + return qc @@ -188,68 +180,65 @@ def circuit5(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (28)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for j in range(4): for i in range(4): if i!=j: qc.crz(theta[count],qr[3-j],qr[3-i]) count=count+1 - + for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - - + + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - - if repeat!=0: + + if repeat!=0: qc.barrier(qr) - - - for l in range(L): - + + for _ in range(L): for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + for j in range(4): for i in range(4): if i!=j: qc.crz(theta[count],qr[j],qr[i]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + return qc @@ -259,68 +248,65 @@ def circuit6(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (28)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for j in range(4): for i in range(4): if i!=j: qc.crx(theta[count],qr[3-j],qr[3-i]) count=count+1 - + for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - - + + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - - if repeat!=0: + + if repeat!=0: qc.barrier(qr) - - - for l in range(L): - + + for _ in range(L): for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + for j in range(4): for i in range(4): if i!=j: qc.crx(theta[count],qr[j],qr[i]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + return qc @@ -329,23 +315,22 @@ def circuit7(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (19)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - - count=0 - for l in range(L): + count=0 + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + qc.crz(theta[count],qr[1],qr[0]) count=count+1 @@ -357,51 +342,50 @@ def circuit7(qc,qr,theta,L,repeat): for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - - + + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - - qc.crz(theta[count],qr[2],qr[1]) + + qc.crz(theta[count],qr[2],qr[1]) count=count+1 - - - if repeat!=0: + + + if repeat!=0: qc.barrier(qr) - - - for l in range(L): - - qc.crz(theta[count],qr[2],qr[1]) + + + for _ in range(L): + qc.crz(theta[count],qr[2],qr[1]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - - + + qc.crz(theta[count],qr[3],qr[2]) count=count+1 - + qc.crz(theta[count],qr[1],qr[0]) count=count+1 - - - - + + + + for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + return qc @@ -411,23 +395,22 @@ def circuit8(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (19)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - - count=0 - for l in range(L): + count=0 + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + qc.crx(theta[count],qr[1],qr[0]) count=count+1 @@ -439,51 +422,50 @@ def circuit8(qc,qr,theta,L,repeat): for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - - + + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - - qc.crx(theta[count],qr[2],qr[1]) + + qc.crx(theta[count],qr[2],qr[1]) count=count+1 - - - if repeat!=0: + + + if repeat!=0: qc.barrier(qr) - - - for l in range(L): - - qc.crx(theta[count],qr[2],qr[1]) + + + for _ in range(L): + qc.crx(theta[count],qr[2],qr[1]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - - + + qc.crx(theta[count],qr[3],qr[2]) count=count+1 - + qc.crx(theta[count],qr[1],qr[0]) count=count+1 - - - - + + + + for i in range(4): qc.rz(theta[count],qr[i]) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + return qc @@ -492,45 +474,44 @@ def circuit9(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (4)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - - count=0 - for l in range(L): + count=0 + for _ in range(L): for i in range(4): qc.h(qr[i]) qc.cz(qr[3],qr[2]) qc.cz(qr[2],qr[1]) qc.cz(qr[1],qr[0]) - + for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - - - if repeat!=0: + + + if repeat!=0: qc.barrier(qr) - - - for l in range(L): + + + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - - qc.cz(qr[1],qr[0]) + + qc.cz(qr[1],qr[0]) qc.cz(qr[2],qr[1]) qc.cz(qr[3],qr[2]) - - - + + + for i in range(4): qc.h(qr[i]) - + return qc @@ -544,20 +525,19 @@ def circuit10(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (4)L+4 #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - + count=0 - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - for l in range(L): + for _ in range(L): qc.cz(qr[3],qr[2]) qc.cz(qr[2],qr[1]) qc.cz(qr[1],qr[0]) @@ -565,32 +545,32 @@ def circuit10(qc,qr,theta,L,repeat): - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - - if repeat!=0: + + + if repeat!=0: qc.barrier(qr) - for l in range(L): + for _ in range(L): for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - qc.cz(qr[3],qr[0]) + + qc.cz(qr[3],qr[0]) qc.cz(qr[1],qr[0]) qc.cz(qr[2],qr[1]) qc.cz(qr[3],qr[2]) - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - - - + + + + return qc @@ -601,22 +581,21 @@ def circuit11(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (12)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + qc.cx(qr[1],qr[0]) qc.cx(qr[3],qr[2]) @@ -624,45 +603,45 @@ def circuit11(qc,qr,theta,L,repeat): qc.ry(theta[count],qr[1]) - count=count+1 + count=count+1 qc.ry(theta[count],qr[2]) - count=count+1 + count=count+1 qc.rz(theta[count],qr[1]) - count=count+1 + count=count+1 qc.rz(theta[count],qr[2]) - count=count+1 + count=count+1 qc.cx(qr[2],qr[1]) - - - if repeat!=0: + + + if repeat!=0: qc.barrier(qr) - for l in range(L): + for _ in range(L): qc.cx(qr[2],qr[1]) - + qc.rz(theta[count],qr[2]) - count=count+1 + count=count+1 qc.rz(theta[count],qr[1]) - count=count+1 + count=count+1 qc.ry(theta[count],qr[2]) - count=count+1 + count=count+1 qc.ry(theta[count],qr[1]) count=count+1 - + qc.cx(qr[3],qr[2]) qc.cx(qr[1],qr[0]) - - + + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - - + + + return qc @@ -678,22 +657,21 @@ def circuit12(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (12)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + qc.cz(qr[1],qr[0]) qc.cz(qr[3],qr[2]) @@ -701,45 +679,45 @@ def circuit12(qc,qr,theta,L,repeat): qc.ry(theta[count],qr[1]) - count=count+1 + count=count+1 qc.ry(theta[count],qr[2]) - count=count+1 + count=count+1 qc.rz(theta[count],qr[1]) - count=count+1 + count=count+1 qc.rz(theta[count],qr[2]) - count=count+1 + count=count+1 qc.cz(qr[2],qr[1]) - - - if repeat!=0: + + + if repeat!=0: qc.barrier(qr) - for l in range(L): + for _ in range(L): qc.cz(qr[2],qr[1]) - + qc.rz(theta[count],qr[2]) - count=count+1 + count=count+1 qc.rz(theta[count],qr[1]) - count=count+1 + count=count+1 qc.ry(theta[count],qr[2]) - count=count+1 + count=count+1 qc.ry(theta[count],qr[1]) count=count+1 - + qc.cz(qr[3],qr[2]) qc.cz(qr[1],qr[0]) - - + + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - - + + + return qc @@ -749,77 +727,76 @@ def circuit13(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (16)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 qc.crz(theta[count],qr[3],qr[0]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[2],qr[3]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[1],qr[2]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[0],qr[1]) count=count+1 - - + + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 qc.crz(theta[count],qr[3],qr[2]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[0],qr[3]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[1],qr[0]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[2],qr[1]) count=count+1 - - if repeat!=0: + + if repeat!=0: qc.barrier(qr) - for l in range(L): - + for _ in range(L): qc.crz(theta[count],qr[2],qr[1]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[1],qr[0]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[0],qr[3]) count=count+1 - + qc.crz(theta[count],qr[3],qr[2]) count=count+1 - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 qc.crz(theta[count],qr[0],qr[1]) count=count+1 - + qc.crz(theta[count],qr[1],qr[2]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[2],qr[3]) count=count+1 - + qc.crz(theta[count],qr[3],qr[0]) count=count+1 - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - + + return qc @@ -830,77 +807,76 @@ def circuit14(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (16)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 qc.crx(theta[count],qr[3],qr[0]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[2],qr[3]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[1],qr[2]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[0],qr[1]) count=count+1 - - + + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 qc.crx(theta[count],qr[3],qr[2]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[0],qr[3]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[1],qr[0]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[2],qr[1]) count=count+1 - - if repeat!=0: + + if repeat!=0: qc.barrier(qr) - for l in range(L): - + for _ in range(L): qc.crx(theta[count],qr[2],qr[1]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[1],qr[0]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[0],qr[3]) count=count+1 - + qc.crx(theta[count],qr[3],qr[2]) count=count+1 - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 qc.crx(theta[count],qr[0],qr[1]) count=count+1 - + qc.crx(theta[count],qr[1],qr[2]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[2],qr[3]) count=count+1 - + qc.crx(theta[count],qr[3],qr[0]) count=count+1 - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - + + return qc @@ -910,13 +886,13 @@ def circuit15(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (8)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 @@ -933,7 +909,7 @@ def circuit15(qc,qr,theta,L,repeat): - + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 @@ -946,40 +922,39 @@ def circuit15(qc,qr,theta,L,repeat): qc.cx(qr[1],qr[0]) qc.cx(qr[2],qr[1]) - - if repeat!=0: + + if repeat!=0: qc.barrier(qr) - for l in range(L): - + for _ in range(L): qc.cx(qr[2],qr[1]) qc.cx(qr[1],qr[0]) - + qc.cx(qr[0],qr[3]) - + qc.cx(qr[3],qr[2]) - - + + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 qc.cx(qr[0],qr[1]) - + qc.cx(qr[1],qr[2]) - + qc.cx(qr[2],qr[3]) - + qc.cx(qr[3],qr[0]) - - + + for i in range(4): qc.ry(theta[count],qr[i]) count=count+1 - - + + return qc @@ -990,21 +965,21 @@ def circuit16(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (11)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + qc.crz(theta[count],qr[1],qr[0]) count=count+1 @@ -1012,14 +987,12 @@ def circuit16(qc,qr,theta,L,repeat): count=count+1 qc.crz(theta[count],qr[2],qr[1]) count=count+1 - - if repeat!=0: - qc.barrier(qr) - for l in range(L): - + if repeat!=0: + qc.barrier(qr) + for _ in range(L): qc.crz(theta[count],qr[2],qr[1]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[3],qr[2]) count=count+1 qc.crz(theta[count],qr[1],qr[0]) @@ -1041,21 +1014,21 @@ def circuit17(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (11)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + qc.crx(theta[count],qr[1],qr[0]) count=count+1 @@ -1063,14 +1036,12 @@ def circuit17(qc,qr,theta,L,repeat): count=count+1 qc.crx(theta[count],qr[2],qr[1]) count=count+1 - - if repeat!=0: - qc.barrier(qr) - for l in range(L): - + if repeat!=0: + qc.barrier(qr) + for _ in range(L): qc.crx(theta[count],qr[2],qr[1]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[3],qr[2]) count=count+1 qc.crx(theta[count],qr[1],qr[0]) @@ -1096,22 +1067,21 @@ def circuit18(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (12)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + qc.crz(theta[count],qr[3],qr[0]) @@ -1122,14 +1092,12 @@ def circuit18(qc,qr,theta,L,repeat): count=count+1 qc.crz(theta[count],qr[0],qr[1]) count=count+1 - - if repeat!=0: - qc.barrier(qr) - for l in range(L): - + if repeat!=0: + qc.barrier(qr) + for _ in range(L): qc.crz(theta[count],qr[0],qr[1]) - count=count+1 + count=count+1 qc.crz(theta[count],qr[1],qr[2]) count=count+1 qc.crz(theta[count],qr[2],qr[3]) @@ -1154,22 +1122,21 @@ def circuit19(qc,qr,theta,L,repeat): #theta is list of the parameters #theta length is (12)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],qr[i]) count=count+1 - + for i in range(4): qc.rz(theta[count],qr[i]) count=count+1 - + qc.crx(theta[count],qr[3],qr[0]) @@ -1180,14 +1147,12 @@ def circuit19(qc,qr,theta,L,repeat): count=count+1 qc.crx(theta[count],qr[0],qr[1]) count=count+1 - - if repeat!=0: - qc.barrier(qr) - for l in range(L): - + if repeat!=0: + qc.barrier(qr) + for _ in range(L): qc.crx(theta[count],qr[0],qr[1]) - count=count+1 + count=count+1 qc.crx(theta[count],qr[1],qr[2]) count=count+1 qc.crx(theta[count],qr[2],qr[3]) diff --git a/Pyfiles/circuits_n1.py b/Pyfiles/circuits_n1.py index da4b55f..6ab5aa2 100644 --- a/Pyfiles/circuits_n1.py +++ b/Pyfiles/circuits_n1.py @@ -6,33 +6,31 @@ def circuit1(qc,theta,L,repeat): #theta is list of the parameters #theta length is 8L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],i) count=count+1 for i in range(4): qc.rz(theta[count],i) count=count+1 - + if repeat!=0: - - for l in range(L): + for _ in range(L): for i in range(4): qc.rz(theta[count],i) count=count+1 - + for i in range(4): qc.rx(theta[count],i) count=count+1 - + return qc @@ -41,40 +39,38 @@ def circuit2(qc,theta,L,repeat): #theta is list of the parameters #theta length is 8L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 qc.cx(3,2) qc.cx(2,1) qc.cx(1,0) - - - if repeat!=0: - - for l in range(L): + + + if repeat!=0: + for _ in range(L): qc.cx(1,0) qc.cx(2,1) qc.cx(3,2) - + for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],i) count=count+1 - + return qc @@ -85,46 +81,44 @@ def circuit3(qc,theta,L,repeat): #theta is list of the parameters #theta length is (11)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 qc.crz(theta[count],3,2) - count=count+1 + count=count+1 qc.crz(theta[count],2,1) - count=count+1 + count=count+1 qc.crz(theta[count],1,0) count=count+1 - if repeat!=0: - - + if repeat!=0: - for l in range(L): + + for _ in range(L): qc.crz(theta[count],1,0) - count=count+1 + count=count+1 qc.crz(theta[count],2,1) - count=count+1 + count=count+1 qc.crz(theta[count],3,2) count=count+1 - + for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],i) count=count+1 - + return qc @@ -134,47 +128,45 @@ def circuit4(qc,theta,L,repeat): #theta is list of the parameters #theta length is (11)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - - count=0 - for l in range(L): + count=0 + for _ in range(L): for i in range(4): qc.rx(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 qc.crx(theta[count],3,2) - count=count+1 + count=count+1 qc.crx(theta[count],2,1) - count=count+1 + count=count+1 qc.crx(theta[count],1,0) count=count+1 - if repeat!=0: - - + if repeat!=0: - for l in range(L): + + for _ in range(L): qc.crx(theta[count],1,0) - count=count+1 + count=count+1 qc.crx(theta[count],2,1) - count=count+1 + count=count+1 qc.crx(theta[count],3,2) count=count+1 - + for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],i) count=count+1 - + return qc @@ -185,68 +177,65 @@ def circuit5(qc,theta,L,repeat): #theta is list of the parameters #theta length is (28)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],i) count=count+1 - + for j in range(4): for i in range(4): if i!=j: qc.crz(theta[count],3-j,3-i) count=count+1 - + for i in range(4): qc.rx(theta[count],i) count=count+1 - - + + for i in range(4): qc.rz(theta[count],i) count=count+1 - - if repeat!=0: - - - - for l in range(L): + + if repeat!=0: + + for _ in range(L): for i in range(4): qc.rz(theta[count],i) count=count+1 - + for i in range(4): qc.rx(theta[count],i) count=count+1 - + for j in range(4): for i in range(4): if i!=j: qc.crz(theta[count],j,i) count=count+1 - + for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],i) count=count+1 - + return qc @@ -256,68 +245,65 @@ def circuit6(qc,theta,L,repeat): #theta is list of the parameters #theta length is (28)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],i) count=count+1 - + for j in range(4): for i in range(4): if i!=j: qc.crx(theta[count],3-j,3-i) count=count+1 - + for i in range(4): qc.rx(theta[count],i) count=count+1 - - + + for i in range(4): qc.rz(theta[count],i) count=count+1 - - if repeat!=0: - - - - for l in range(L): + + if repeat!=0: + + for _ in range(L): for i in range(4): qc.rz(theta[count],i) count=count+1 - + for i in range(4): qc.rx(theta[count],i) count=count+1 - + for j in range(4): for i in range(4): if i!=j: qc.crx(theta[count],j,i) count=count+1 - + for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],i) count=count+1 - + return qc @@ -326,23 +312,22 @@ def circuit7(qc,theta,L,repeat): #theta is list of the parameters #theta length is (19)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - - count=0 - for l in range(L): + count=0 + for _ in range(L): for i in range(4): qc.rx(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],i) count=count+1 - + qc.crz(theta[count],1,0) count=count+1 @@ -354,51 +339,50 @@ def circuit7(qc,theta,L,repeat): for i in range(4): qc.rx(theta[count],i) count=count+1 - - + + for i in range(4): qc.rz(theta[count],i) count=count+1 - - qc.crz(theta[count],2,1) + + qc.crz(theta[count],2,1) count=count+1 + + + if repeat!=0: - - if repeat!=0: - - - - for l in range(L): + - qc.crz(theta[count],2,1) + for _ in range(L): + qc.crz(theta[count],2,1) count=count+1 - + for i in range(4): qc.rz(theta[count],i) count=count+1 - + for i in range(4): qc.rx(theta[count],i) count=count+1 - - + + qc.crz(theta[count],3,2) count=count+1 - + qc.crz(theta[count],1,0) count=count+1 - - - - + + + + for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],i) count=count+1 - + return qc @@ -408,23 +392,22 @@ def circuit8(qc,theta,L,repeat): #theta is list of the parameters #theta length is (19)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - - count=0 - for l in range(L): + count=0 + for _ in range(L): for i in range(4): qc.rx(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rz(theta[count],i) count=count+1 - + qc.crx(theta[count],1,0) count=count+1 @@ -436,51 +419,50 @@ def circuit8(qc,theta,L,repeat): for i in range(4): qc.rx(theta[count],i) count=count+1 - - + + for i in range(4): qc.rz(theta[count],i) count=count+1 - - qc.crx(theta[count],2,1) + + qc.crx(theta[count],2,1) count=count+1 + + + if repeat!=0: - - if repeat!=0: - - - - for l in range(L): + - qc.crx(theta[count],2,1) + for _ in range(L): + qc.crx(theta[count],2,1) count=count+1 - + for i in range(4): qc.rz(theta[count],i) count=count+1 - + for i in range(4): qc.rx(theta[count],i) count=count+1 - - + + qc.crx(theta[count],3,2) count=count+1 - + qc.crx(theta[count],1,0) count=count+1 - - - - + + + + for i in range(4): qc.rz(theta[count],i) - count=count+1 + count=count+1 for i in range(4): qc.rx(theta[count],i) count=count+1 - + return qc @@ -489,45 +471,44 @@ def circuit9(qc,theta,L,repeat): #theta is list of the parameters #theta length is (4)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - - count=0 - for l in range(L): + count=0 + for _ in range(L): for i in range(4): qc.h(i) qc.cz(3,2) qc.cz(2,1) qc.cz(1,0) - + for i in range(4): qc.rx(theta[count],i) count=count+1 + + + if repeat!=0: - - if repeat!=0: - - + - for l in range(L): + for _ in range(L): for i in range(4): qc.rx(theta[count],i) count=count+1 - - qc.cz(1,0) + + qc.cz(1,0) qc.cz(2,1) qc.cz(3,2) - - - + + + for i in range(4): qc.h(i) - + return qc @@ -541,20 +522,19 @@ def circuit10(qc,theta,L,repeat): #theta is list of the parameters #theta length is (4)L+4 #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat - + count=0 - + for i in range(4): qc.ry(theta[count],i) count=count+1 - - for l in range(L): + for _ in range(L): qc.cz(3,2) qc.cz(2,1) qc.cz(1,0) @@ -562,32 +542,32 @@ def circuit10(qc,theta,L,repeat): - + for i in range(4): qc.ry(theta[count],i) count=count+1 + + + if repeat!=0: - - if repeat!=0: - - for l in range(L): + for _ in range(L): for i in range(4): qc.ry(theta[count],i) count=count+1 - - qc.cz(3,0) + + qc.cz(3,0) qc.cz(1,0) qc.cz(2,1) qc.cz(3,2) - + for i in range(4): qc.ry(theta[count],i) count=count+1 - - - - + + + + return qc @@ -598,22 +578,21 @@ def circuit11(qc,theta,L,repeat): #theta is list of the parameters #theta length is (12)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.ry(theta[count],i) count=count+1 - + for i in range(4): qc.rz(theta[count],i) count=count+1 - + qc.cx(1,0) qc.cx(3,2) @@ -621,45 +600,45 @@ def circuit11(qc,theta,L,repeat): qc.ry(theta[count],1) - count=count+1 + count=count+1 qc.ry(theta[count],2) - count=count+1 + count=count+1 qc.rz(theta[count],1) - count=count+1 + count=count+1 qc.rz(theta[count],2) - count=count+1 + count=count+1 qc.cx(2,1) + + + if repeat!=0: - - if repeat!=0: - - for l in range(L): + for _ in range(L): qc.cx(2,1) - + qc.rz(theta[count],2) - count=count+1 + count=count+1 qc.rz(theta[count],1) - count=count+1 + count=count+1 qc.ry(theta[count],2) - count=count+1 + count=count+1 qc.ry(theta[count],1) count=count+1 - + qc.cx(3,2) qc.cx(1,0) - - + + for i in range(4): qc.rz(theta[count],i) count=count+1 - + for i in range(4): qc.ry(theta[count],i) count=count+1 - - - + + + return qc @@ -675,22 +654,21 @@ def circuit12(qc,theta,L,repeat): #theta is list of the parameters #theta length is (12)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.ry(theta[count],i) count=count+1 - + for i in range(4): qc.rz(theta[count],i) count=count+1 - + qc.cz(1,0) qc.cz(3,2) @@ -698,45 +676,45 @@ def circuit12(qc,theta,L,repeat): qc.ry(theta[count],1) - count=count+1 + count=count+1 qc.ry(theta[count],2) - count=count+1 + count=count+1 qc.rz(theta[count],1) - count=count+1 + count=count+1 qc.rz(theta[count],2) - count=count+1 + count=count+1 qc.cz(2,1) + + + if repeat!=0: - - if repeat!=0: - - for l in range(L): + for _ in range(L): qc.cz(2,1) - + qc.rz(theta[count],2) - count=count+1 + count=count+1 qc.rz(theta[count],1) - count=count+1 + count=count+1 qc.ry(theta[count],2) - count=count+1 + count=count+1 qc.ry(theta[count],1) count=count+1 - + qc.cz(3,2) qc.cz(1,0) - - + + for i in range(4): qc.rz(theta[count],i) count=count+1 - + for i in range(4): qc.ry(theta[count],i) count=count+1 - - - + + + return qc @@ -746,77 +724,76 @@ def circuit13(qc,theta,L,repeat): #theta is list of the parameters #theta length is (16)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.ry(theta[count],i) count=count+1 qc.crz(theta[count],3,0) - count=count+1 + count=count+1 qc.crz(theta[count],2,3) - count=count+1 + count=count+1 qc.crz(theta[count],1,2) - count=count+1 + count=count+1 qc.crz(theta[count],0,1) count=count+1 - - + + for i in range(4): qc.ry(theta[count],i) count=count+1 qc.crz(theta[count],3,2) - count=count+1 + count=count+1 qc.crz(theta[count],0,3) - count=count+1 + count=count+1 qc.crz(theta[count],1,0) - count=count+1 + count=count+1 qc.crz(theta[count],2,1) count=count+1 - - if repeat!=0: + + if repeat!=0: - for l in range(L): - + for _ in range(L): qc.crz(theta[count],2,1) - count=count+1 + count=count+1 qc.crz(theta[count],1,0) - count=count+1 + count=count+1 qc.crz(theta[count],0,3) count=count+1 - + qc.crz(theta[count],3,2) count=count+1 - + for i in range(4): qc.ry(theta[count],i) count=count+1 qc.crz(theta[count],0,1) count=count+1 - + qc.crz(theta[count],1,2) - count=count+1 + count=count+1 qc.crz(theta[count],2,3) count=count+1 - + qc.crz(theta[count],3,0) count=count+1 - + for i in range(4): qc.ry(theta[count],i) count=count+1 - - + + return qc @@ -827,77 +804,76 @@ def circuit14(qc,theta,L,repeat): #theta is list of the parameters #theta length is (16)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.ry(theta[count],i) count=count+1 qc.crx(theta[count],3,0) - count=count+1 + count=count+1 qc.crx(theta[count],2,3) - count=count+1 + count=count+1 qc.crx(theta[count],1,2) - count=count+1 + count=count+1 qc.crx(theta[count],0,1) count=count+1 - - + + for i in range(4): qc.ry(theta[count],i) count=count+1 qc.crx(theta[count],3,2) - count=count+1 + count=count+1 qc.crx(theta[count],0,3) - count=count+1 + count=count+1 qc.crx(theta[count],1,0) - count=count+1 + count=count+1 qc.crx(theta[count],2,1) count=count+1 - - if repeat!=0: + + if repeat!=0: - for l in range(L): - + for _ in range(L): qc.crx(theta[count],2,1) - count=count+1 + count=count+1 qc.crx(theta[count],1,0) - count=count+1 + count=count+1 qc.crx(theta[count],0,3) count=count+1 - + qc.crx(theta[count],3,2) count=count+1 - + for i in range(4): qc.ry(theta[count],i) count=count+1 qc.crx(theta[count],0,1) count=count+1 - + qc.crx(theta[count],1,2) - count=count+1 + count=count+1 qc.crx(theta[count],2,3) count=count+1 - + qc.crx(theta[count],3,0) count=count+1 - + for i in range(4): qc.ry(theta[count],i) count=count+1 - - + + return qc @@ -907,13 +883,13 @@ def circuit15(qc,theta,L,repeat): #theta is list of the parameters #theta length is (8)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.ry(theta[count],i) count=count+1 @@ -930,7 +906,7 @@ def circuit15(qc,theta,L,repeat): - + for i in range(4): qc.ry(theta[count],i) count=count+1 @@ -943,40 +919,39 @@ def circuit15(qc,theta,L,repeat): qc.cx(1,0) qc.cx(2,1) - - if repeat!=0: + + if repeat!=0: - for l in range(L): - + for _ in range(L): qc.cx(2,1) qc.cx(1,0) - + qc.cx(0,3) - + qc.cx(3,2) - - + + for i in range(4): qc.ry(theta[count],i) count=count+1 qc.cx(0,1) - + qc.cx(1,2) - + qc.cx(2,3) - + qc.cx(3,0) - - + + for i in range(4): qc.ry(theta[count],i) count=count+1 - - + + return qc @@ -987,21 +962,21 @@ def circuit16(qc,theta,L,repeat): #theta is list of the parameters #theta length is (11)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.rx(theta[count],i) count=count+1 - + for i in range(4): qc.rz(theta[count],i) count=count+1 - + qc.crz(theta[count],1,0) count=count+1 @@ -1009,14 +984,12 @@ def circuit16(qc,theta,L,repeat): count=count+1 qc.crz(theta[count],2,1) count=count+1 - - if repeat!=0: - - for l in range(L): - + if repeat!=0: + + for _ in range(L): qc.crz(theta[count],2,1) - count=count+1 + count=count+1 qc.crz(theta[count],3,2) count=count+1 qc.crz(theta[count],1,0) @@ -1038,21 +1011,21 @@ def circuit17(qc,theta,L,repeat): #theta is list of the parameters #theta length is (11)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): + for _ in range(L): for i in range(4): qc.rx(theta[count],i) count=count+1 - + for i in range(4): qc.rz(theta[count],i) count=count+1 - + qc.crx(theta[count],1,0) count=count+1 @@ -1060,14 +1033,12 @@ def circuit17(qc,theta,L,repeat): count=count+1 qc.crx(theta[count],2,1) count=count+1 - - if repeat!=0: - - for l in range(L): - + if repeat!=0: + + for _ in range(L): qc.crx(theta[count],2,1) - count=count+1 + count=count+1 qc.crx(theta[count],3,2) count=count+1 qc.crx(theta[count],1,0) @@ -1093,22 +1064,21 @@ def circuit18(qc,theta,L,repeat): #theta is list of the parameters #theta length is (12)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],i) count=count+1 - + for i in range(4): qc.rz(theta[count],i) count=count+1 - + qc.crz(theta[count],3,0) @@ -1119,14 +1089,12 @@ def circuit18(qc,theta,L,repeat): count=count+1 qc.crz(theta[count],0,1) count=count+1 - - if repeat!=0: - - for l in range(L): - + if repeat!=0: + + for _ in range(L): qc.crz(theta[count],0,1) - count=count+1 + count=count+1 qc.crz(theta[count],1,2) count=count+1 qc.crz(theta[count],2,3) @@ -1151,22 +1119,21 @@ def circuit19(qc,theta,L,repeat): #theta is list of the parameters #theta length is (12)L #L is the number of repeatation - + # repeat will conjugate the first part and add next the the circuit for expressibility # 0:No, 1: Repeat count=0 - for l in range(L): - + for _ in range(L): for i in range(4): qc.rx(theta[count],i) count=count+1 - + for i in range(4): qc.rz(theta[count],i) count=count+1 - + qc.crx(theta[count],3,0) @@ -1177,14 +1144,12 @@ def circuit19(qc,theta,L,repeat): count=count+1 qc.crx(theta[count],0,1) count=count+1 - - if repeat!=0: - - for l in range(L): - + if repeat!=0: + + for _ in range(L): qc.crx(theta[count],0,1) - count=count+1 + count=count+1 qc.crx(theta[count],1,2) count=count+1 qc.crx(theta[count],2,3) diff --git a/Pyfiles/experiments.py b/Pyfiles/experiments.py index 6991830..7f8d189 100644 --- a/Pyfiles/experiments.py +++ b/Pyfiles/experiments.py @@ -45,7 +45,7 @@ def make_embedding_circuit(): def make_classifer_circuit(ID): r'''A helper function which makes the classfier circuits. Given an ID, it returns the PQC.''' - param_y=[(Parameter('θ'+str(i))) for i in range(40)] + param_y = [Parameter(f'θ{str(i)}') for i in range(40)] ansatz = QuantumCircuit(4, name='PQC') ansatz=globals()["circuit"+"{0}".format(ID)](ansatz,param_y,1,0) # Remove this word once you've filled in the code. @@ -62,12 +62,7 @@ def load_data(dataSetID): dataCoords = np.loadtxt(dataPath) dataLabels = np.loadtxt(dataLabel) - # Make a data structure which is easier to work with - # for shuffling. - # Also, notice we change the data labels from {0, 1} to {-1, +1} - data = list(zip(dataCoords, 2*dataLabels-1)) - - return data + return list(zip(dataCoords, 2*dataLabels-1)) def generate_train_validate_test_data(data, train_size=100, validate_size=500, test_size=None, randomSeed=0): r'''This is a function which, given a dataset, will return 3 distinct datasets from it: a training dataset, @@ -146,7 +141,7 @@ def train_model(feature_map, ansatz, epochs, learning_rate, train_X, train_y, qi interpret=parity, output_shape=output_shape, quantum_instance=qi) # Set up the pyTorch model - np.random.seed(0) + np.random.seed(0) initial_weights = 0.1*(2*np.random.rand(qnn.num_weights) - 1) model = TorchConnector(qnn, initial_weights) @@ -155,20 +150,20 @@ def train_model(feature_map, ansatz, epochs, learning_rate, train_X, train_y, qi f_loss = MSELoss(reduction='mean') # Set model to training mode - model.train() + model.train() print("__Learning Rate (", learning_rate,") is intialized") - for epoch in range(epochs): + for _ in range(epochs): optimizer.zero_grad() # initialize gradient loss = 0.0 # initialize loss for x, y_target in zip(train_X, train_y): # evaluate batch loss output = model(Tensor(x)).reshape(1, 2) # forward pass targets=Tensor([y_target]).long() targets = targets.to(torch.float32) - loss += f_loss(output, targets) + loss += f_loss(output, targets) loss.backward() # backward pass # run optimizer - optimizer.step() + optimizer.step() print("__Learning Rate (", learning_rate,") is done") return model \ No newline at end of file diff --git a/Pyfiles/optimizer.py b/Pyfiles/optimizer.py index a1e3f37..230e1d9 100644 --- a/Pyfiles/optimizer.py +++ b/Pyfiles/optimizer.py @@ -16,11 +16,11 @@ def SPSA_gradient(loss, currentParams, gradientCoefficient): numParams = len(currentParams) # Generate a random perturbation using the Rademacher distribution randomPerturbation = 2*np.random.binomial(1, .5, size=numParams) - 1 - - gradient = (loss(currentParams + gradientCoefficient*randomPerturbation) - loss(currentParams - gradientCoefficient*randomPerturbation))\ - /(gradientCoefficient*randomPerturbation) - - return gradient + + return ( + loss(currentParams + gradientCoefficient * randomPerturbation) + - loss(currentParams - gradientCoefficient * randomPerturbation) + ) / (gradientCoefficient * randomPerturbation) def SPSA_update(loss, currentParams, updateCoefficient, gradientCoefficient): r'''Performs a parameter update according to the SPSA approach. diff --git a/Pyfiles/quantumcircuit.py b/Pyfiles/quantumcircuit.py index e5f2d8f..3dd927a 100644 --- a/Pyfiles/quantumcircuit.py +++ b/Pyfiles/quantumcircuit.py @@ -51,13 +51,9 @@ def convertv(input_dict): Returns: yprediction''' k = Counter(input_dict) - val=k.most_common(1)[0][0] + val=k.most_common(1)[0][0] list1=['00','11']; - if val in list1: - ypredit=-1 - else: - ypredit=1 - return ypredit + return -1 if val in list1 else 1 @@ -97,8 +93,7 @@ def Qrun(datainput,currentParams,nshot,nqubit): job = execute(qc, backend, shots=nshot) result = job.result() count =result.get_counts() - predict=convertv(count) - return predict + return convertv(count) @@ -112,7 +107,7 @@ def l1loss(predict,datalabel): calculate l1 loss''' return abs(predict-datalabel) -def loss2qubit(datainput,datalabel,currentParams): +def loss2qubit(datainput,datalabel,currentParams): r'''L1 loss assmbler, run the Q circuit, and calcualte the loss for the given data. Inputs: @@ -123,15 +118,12 @@ def loss2qubit(datainput,datalabel,currentParams): Returns: calculate l1 loss''' - nshot=10000; #number of shots on simulation - nqubit=2; # how many qubit? - + nshot=10000 + nqubit=2 predict=Qrun(datainput,currentParams,nshot,nqubit) - loss=l1loss(predict,datalabel) - - return loss + return l1loss(predict,datalabel) -def quick_predict(datainput,currentParams): +def quick_predict(datainput,currentParams): r'''L1 loss assmbler, run the Q circuit, and calcualte the loss for the given data. Inputs: @@ -142,12 +134,9 @@ def quick_predict(datainput,currentParams): Returns: prediction''' - nshot=200; #number of shots on simulation - nqubit=2; # how many qubit? - - predict=Qrun(datainput,currentParams,nshot,nqubit) - - return predict + nshot=200 + nqubit=2 + return Qrun(datainput,currentParams,nshot,nqubit) @@ -166,13 +155,9 @@ def convert_qubitF(input_dict): Returns: yprediction''' k = Counter(input_dict) - val=k.most_common(1)[0][0] + val=k.most_common(1)[0][0] list1=['0000','0001','0010','0011','1100','1101','1110','1111']; - if val in list1: - ypredit=-1 - else: - ypredit=1 - return ypredit + return -1 if val in list1 else 1 @@ -189,12 +174,11 @@ def Qrun_qubitF(datainput,currentParams,nshot,nqubit,circuit): job = execute(qc, backend, shots=nshot) result = job.result() count =result.get_counts() - predict=convert_qubitF(count) - return predict + return convert_qubitF(count) -def loss_qubitF(datainput,datalabel,currentParams,circuit): +def loss_qubitF(datainput,datalabel,currentParams,circuit): r'''L1 loss assmbler, run the Q circuit, and calcualte the loss for the given data. Inputs: @@ -205,15 +189,12 @@ def loss_qubitF(datainput,datalabel,currentParams,circuit): Returns: calculate l1 loss''' - nshot=10000; #number of shots on simulation - nqubit=4; # how many qubit? - + nshot=10000 + nqubit=4 predict=Qrun_qubitF(datainput,currentParams,nshot,nqubit,circuit) - loss=l1loss(predict,datalabel) - - return loss + return l1loss(predict,datalabel) -def predict_qubitF(datainput,currentParams,circuit): +def predict_qubitF(datainput,currentParams,circuit): r'''L1 loss assmbler, run the Q circuit, and calcualte the loss for the given data. Inputs: @@ -224,9 +205,6 @@ def predict_qubitF(datainput,currentParams,circuit): Returns: prediction''' - nshot=200; #number of shots on simulation - nqubit=4; # how many qubit? - - predict=Qrun_qubitF(datainput,currentParams,nshot,nqubit,circuit) - - return predict + nshot=200 + nqubit=4 + return Qrun_qubitF(datainput,currentParams,nshot,nqubit,circuit)