Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 calculcate expected values
|
12 |
+
expval = AerPauliExpectation()
|
13 |
+
|
14 |
+
# define gradient method
|
15 |
+
gradient = Gradient()
|
16 |
+
|
17 |
+
# define quantum instances (statevector and sample based)
|
18 |
+
qi_sv = QuantumInstance(Aer.get_backend("aer_simulator_statevector"))
|
19 |
+
|
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)
|
27 |
+
qc1.h(0)
|
28 |
+
qc1.ry(params1[0], 0)
|
29 |
+
qc1.rx(params1[1], 0)
|
30 |
+
qc_sfn1 = StateFn(qc1)
|
31 |
+
|
32 |
+
# construct cost operator
|
33 |
+
H1 = StateFn(PauliSumOp.from_list([("Z", 1.0), ("X", 1.0)]))
|
34 |
+
|
35 |
+
# combine operator and circuit to objective function
|
36 |
+
op1 = ~H1 @ qc_sfn1
|
37 |
+
print(op1)
|
38 |
+
|
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 |
+
|
48 |
+
# QNN batched forward pass
|
49 |
+
qnn1.forward([input1, input1], weights1)
|
50 |
+
|
51 |
+
# QNN backward pass
|
52 |
+
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")
|
71 |
+
|
72 |
+
# specify the ansatz
|
73 |
+
ansatz = RealAmplitudes(num_qubits, reps=1)
|
74 |
+
ansatz.draw(output="mpl")
|
75 |
+
|
76 |
+
# specify the observable
|
77 |
+
observable = PauliSumOp.from_list([("Z" * num_qubits, 1)])
|
78 |
+
print(observable)
|
79 |
+
|
80 |
+
# define two layer QNN
|
81 |
+
qnn3 = TwoLayerQNN(
|
82 |
+
num_qubits, feature_map=fm, ansatz=ansatz, observable=observable, quantum_instance=qi_sv
|
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 |
+
|
105 |
+
# QNN backward pass, returns a tuple of sparse matrices
|
106 |
+
qnn4.backward(input4, weights4)
|
107 |
+
|
108 |
+
# specify circuit QNN
|
109 |
+
parity = lambda x: "{:b}".format(x).count("1") % 2
|
110 |
+
output_shape = 2 # this is required in case of a callable with dense output
|
111 |
+
qnn6 = CircuitQNN(
|
112 |
+
qc,
|
113 |
+
[],
|
114 |
+
qc.parameters,
|
115 |
+
sparse=False,
|
116 |
+
interpret=parity,
|
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 |
+
|
138 |
+
# QNN backward pass
|
139 |
+
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 |
+
|