Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,18 @@
|
|
1 |
import numpy as np
|
2 |
-
|
3 |
from qiskit import Aer, QuantumCircuit
|
4 |
from qiskit.circuit import Parameter
|
5 |
from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap
|
6 |
from qiskit.opflow import StateFn, PauliSumOp, AerPauliExpectation, ListOp, Gradient
|
7 |
from qiskit.utils import QuantumInstance, algorithm_globals
|
|
|
|
|
|
|
8 |
|
|
|
9 |
algorithm_globals.random_seed = 42
|
10 |
|
11 |
-
# set method to
|
12 |
expval = AerPauliExpectation()
|
13 |
|
14 |
# define gradient method
|
@@ -20,7 +24,6 @@ qi_sv = QuantumInstance(Aer.get_backend("aer_simulator_statevector"))
|
|
20 |
# we set shots to 10 as this will determine the number of samples later on.
|
21 |
qi_qasm = QuantumInstance(Aer.get_backend("aer_simulator"), shots=10)
|
22 |
|
23 |
-
from qiskit_machine_learning.neural_networks import OpflowQNN
|
24 |
# construct parametrized circuit
|
25 |
params1 = [Parameter("input1"), Parameter("weight1")]
|
26 |
qc1 = QuantumCircuit(1)
|
@@ -39,9 +42,11 @@ print(op1)
|
|
39 |
# construct OpflowQNN with the operator, the input parameters, the weight parameters,
|
40 |
# the expected value, gradient, and quantum instance.
|
41 |
qnn1 = OpflowQNN(op1, [params1[0]], [params1[1]], expval, gradient, qi_sv)
|
|
|
42 |
# define (random) input and weights
|
43 |
input1 = algorithm_globals.random.random(qnn1.num_inputs)
|
44 |
weights1 = algorithm_globals.random.random(qnn1.num_weights)
|
|
|
45 |
# QNN forward pass
|
46 |
qnn1.forward(input1, weights1)
|
47 |
|
@@ -53,18 +58,18 @@ qnn1.backward(input1, weights1)
|
|
53 |
|
54 |
# QNN batched backward pass
|
55 |
qnn1.backward([input1, input1], weights1)
|
56 |
-
|
57 |
op2 = ListOp([op1, op1])
|
58 |
qnn2 = OpflowQNN(op2, [params1[0]], [params1[1]], expval, gradient, qi_sv)
|
|
|
59 |
# QNN forward pass
|
60 |
qnn2.forward(input1, weights1)
|
61 |
|
62 |
# QNN backward pass
|
63 |
qnn2.backward(input1, weights1)
|
64 |
|
65 |
-
from qiskit_machine_learning.neural_networks import TwoLayerQNN
|
66 |
# specify the number of qubits
|
67 |
num_qubits = 3
|
|
|
68 |
# specify the feature map
|
69 |
fm = ZZFeatureMap(num_qubits, reps=2)
|
70 |
fm.draw(output="mpl")
|
@@ -78,27 +83,27 @@ observable = PauliSumOp.from_list([("Z" * num_qubits, 1)])
|
|
78 |
print(observable)
|
79 |
|
80 |
# define two layer QNN
|
81 |
-
qnn3 = TwoLayerQNN(
|
82 |
-
|
83 |
-
)
|
84 |
# define (random) input and weights
|
85 |
input3 = algorithm_globals.random.random(qnn3.num_inputs)
|
86 |
weights3 = algorithm_globals.random.random(qnn3.num_weights)
|
|
|
87 |
# QNN forward pass
|
88 |
qnn3.forward(input3, weights3)
|
89 |
|
90 |
# QNN backward pass
|
91 |
qnn3.backward(input3, weights3)
|
92 |
-
|
93 |
-
from qiskit_machine_learning.neural_networks import CircuitQNN
|
94 |
qc = RealAmplitudes(num_qubits, entanglement="linear", reps=1)
|
95 |
qc.draw(output="mpl")
|
96 |
|
97 |
# specify circuit QNN
|
98 |
qnn4 = CircuitQNN(qc, [], qc.parameters, sparse=True, quantum_instance=qi_qasm)
|
|
|
99 |
# define (random) input and weights
|
100 |
input4 = algorithm_globals.random.random(qnn4.num_inputs)
|
101 |
weights4 = algorithm_globals.random.random(qnn4.num_weights)
|
|
|
102 |
# QNN forward pass
|
103 |
qnn4.forward(input4, weights4).todense() # returned as a sparse matrix
|
104 |
|
@@ -117,21 +122,24 @@ qnn6 = CircuitQNN(
|
|
117 |
output_shape=output_shape,
|
118 |
quantum_instance=qi_qasm,
|
119 |
)
|
|
|
120 |
# define (random) input and weights
|
121 |
input6 = algorithm_globals.random.random(qnn6.num_inputs)
|
122 |
weights6 = algorithm_globals.random.random(qnn6.num_weights)
|
|
|
123 |
# QNN forward pass
|
124 |
qnn6.forward(input6, weights6)
|
125 |
|
126 |
-
|
127 |
# QNN backward pass
|
128 |
qnn6.backward(input6, weights6)
|
129 |
|
130 |
# specify circuit QNN
|
131 |
qnn7 = CircuitQNN(qc, [], qc.parameters, sampling=True, quantum_instance=qi_qasm)
|
|
|
132 |
# define (random) input and weights
|
133 |
input7 = algorithm_globals.random.random(qnn7.num_inputs)
|
134 |
weights7 = algorithm_globals.random.random(qnn7.num_weights)
|
|
|
135 |
# QNN forward pass, results in samples of measured bit strings mapped to integers
|
136 |
qnn7.forward(input7, weights7)
|
137 |
|
@@ -140,17 +148,15 @@ qnn7.backward(input7, weights7)
|
|
140 |
|
141 |
# specify circuit QNN
|
142 |
qnn8 = CircuitQNN(qc, [], qc.parameters, sampling=True, interpret=parity, quantum_instance=qi_qasm)
|
|
|
143 |
# define (random) input and weights
|
144 |
input8 = algorithm_globals.random.random(qnn8.num_inputs)
|
145 |
weights8 = algorithm_globals.random.random(qnn8.num_weights)
|
|
|
146 |
# QNN forward pass, results in samples of measured bit strings
|
147 |
qnn8.forward(input8, weights8)
|
148 |
|
149 |
# QNN backward pass
|
150 |
qnn8.backward(input8, weights8)
|
151 |
|
152 |
-
import qiskit.tools.jupyter
|
153 |
-
|
154 |
-
#%qiskit_version_table
|
155 |
-
#%qiskit_copyright
|
156 |
|
|
|
1 |
import numpy as np
|
2 |
+
import qiskit.tools.jupyter
|
3 |
from qiskit import Aer, QuantumCircuit
|
4 |
from qiskit.circuit import Parameter
|
5 |
from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap
|
6 |
from qiskit.opflow import StateFn, PauliSumOp, AerPauliExpectation, ListOp, Gradient
|
7 |
from qiskit.utils import QuantumInstance, algorithm_globals
|
8 |
+
from qiskit_machine_learning.neural_networks import CircuitQNN
|
9 |
+
from qiskit_machine_learning.neural_networks import OpflowQNN
|
10 |
+
from qiskit_machine_learning.neural_networks import TwoLayerQNN
|
11 |
|
12 |
+
#random seed
|
13 |
algorithm_globals.random_seed = 42
|
14 |
|
15 |
+
# set method to calculate expected values
|
16 |
expval = AerPauliExpectation()
|
17 |
|
18 |
# define gradient method
|
|
|
24 |
# we set shots to 10 as this will determine the number of samples later on.
|
25 |
qi_qasm = QuantumInstance(Aer.get_backend("aer_simulator"), shots=10)
|
26 |
|
|
|
27 |
# construct parametrized circuit
|
28 |
params1 = [Parameter("input1"), Parameter("weight1")]
|
29 |
qc1 = QuantumCircuit(1)
|
|
|
42 |
# construct OpflowQNN with the operator, the input parameters, the weight parameters,
|
43 |
# the expected value, gradient, and quantum instance.
|
44 |
qnn1 = OpflowQNN(op1, [params1[0]], [params1[1]], expval, gradient, qi_sv)
|
45 |
+
|
46 |
# define (random) input and weights
|
47 |
input1 = algorithm_globals.random.random(qnn1.num_inputs)
|
48 |
weights1 = algorithm_globals.random.random(qnn1.num_weights)
|
49 |
+
|
50 |
# QNN forward pass
|
51 |
qnn1.forward(input1, weights1)
|
52 |
|
|
|
58 |
|
59 |
# QNN batched backward pass
|
60 |
qnn1.backward([input1, input1], weights1)
|
|
|
61 |
op2 = ListOp([op1, op1])
|
62 |
qnn2 = OpflowQNN(op2, [params1[0]], [params1[1]], expval, gradient, qi_sv)
|
63 |
+
|
64 |
# QNN forward pass
|
65 |
qnn2.forward(input1, weights1)
|
66 |
|
67 |
# QNN backward pass
|
68 |
qnn2.backward(input1, weights1)
|
69 |
|
|
|
70 |
# specify the number of qubits
|
71 |
num_qubits = 3
|
72 |
+
|
73 |
# specify the feature map
|
74 |
fm = ZZFeatureMap(num_qubits, reps=2)
|
75 |
fm.draw(output="mpl")
|
|
|
83 |
print(observable)
|
84 |
|
85 |
# define two layer QNN
|
86 |
+
qnn3 = TwoLayerQNN(num_qubits, feature_map=fm, ansatz=ansatz, observable=observable, quantum_instance=qi_sv)
|
87 |
+
|
|
|
88 |
# define (random) input and weights
|
89 |
input3 = algorithm_globals.random.random(qnn3.num_inputs)
|
90 |
weights3 = algorithm_globals.random.random(qnn3.num_weights)
|
91 |
+
|
92 |
# QNN forward pass
|
93 |
qnn3.forward(input3, weights3)
|
94 |
|
95 |
# QNN backward pass
|
96 |
qnn3.backward(input3, weights3)
|
|
|
|
|
97 |
qc = RealAmplitudes(num_qubits, entanglement="linear", reps=1)
|
98 |
qc.draw(output="mpl")
|
99 |
|
100 |
# specify circuit QNN
|
101 |
qnn4 = CircuitQNN(qc, [], qc.parameters, sparse=True, quantum_instance=qi_qasm)
|
102 |
+
|
103 |
# define (random) input and weights
|
104 |
input4 = algorithm_globals.random.random(qnn4.num_inputs)
|
105 |
weights4 = algorithm_globals.random.random(qnn4.num_weights)
|
106 |
+
|
107 |
# QNN forward pass
|
108 |
qnn4.forward(input4, weights4).todense() # returned as a sparse matrix
|
109 |
|
|
|
122 |
output_shape=output_shape,
|
123 |
quantum_instance=qi_qasm,
|
124 |
)
|
125 |
+
|
126 |
# define (random) input and weights
|
127 |
input6 = algorithm_globals.random.random(qnn6.num_inputs)
|
128 |
weights6 = algorithm_globals.random.random(qnn6.num_weights)
|
129 |
+
|
130 |
# QNN forward pass
|
131 |
qnn6.forward(input6, weights6)
|
132 |
|
|
|
133 |
# QNN backward pass
|
134 |
qnn6.backward(input6, weights6)
|
135 |
|
136 |
# specify circuit QNN
|
137 |
qnn7 = CircuitQNN(qc, [], qc.parameters, sampling=True, quantum_instance=qi_qasm)
|
138 |
+
|
139 |
# define (random) input and weights
|
140 |
input7 = algorithm_globals.random.random(qnn7.num_inputs)
|
141 |
weights7 = algorithm_globals.random.random(qnn7.num_weights)
|
142 |
+
|
143 |
# QNN forward pass, results in samples of measured bit strings mapped to integers
|
144 |
qnn7.forward(input7, weights7)
|
145 |
|
|
|
148 |
|
149 |
# specify circuit QNN
|
150 |
qnn8 = CircuitQNN(qc, [], qc.parameters, sampling=True, interpret=parity, quantum_instance=qi_qasm)
|
151 |
+
|
152 |
# define (random) input and weights
|
153 |
input8 = algorithm_globals.random.random(qnn8.num_inputs)
|
154 |
weights8 = algorithm_globals.random.random(qnn8.num_weights)
|
155 |
+
|
156 |
# QNN forward pass, results in samples of measured bit strings
|
157 |
qnn8.forward(input8, weights8)
|
158 |
|
159 |
# QNN backward pass
|
160 |
qnn8.backward(input8, weights8)
|
161 |
|
|
|
|
|
|
|
|
|
162 |
|