-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvariational.py
More file actions
36 lines (28 loc) · 1.14 KB
/
variational.py
File metadata and controls
36 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('default.qubit', wires=1)
@qml.qnode(dev)
def circuit(params):
qml.RY(params[0], wires=0)
return qml.expval.PauliZ(0)
print('The expectation value {}'.format(circuit([0])))
print('The expectation value {}'.format(circuit([np.pi/3])))
print('The expectation value {}'.format(circuit([np.pi])))
def objective(var):
return circuit(var)
np.random.seed(2019)
initial_theta = 2*np.pi*np.random.random_sample()
init_params = np.array([initial_theta])
print('Initial objective function value {:.7f} for theta={:.2f}'.format(objective(init_params),
initial_theta))
# Initilize Gradient Descent Optimizer
opt = qml.GradientDescentOptimizer(stepsize=0.4)
# set the number of steps
steps = 30
# set the initial parameter values
params = init_params
for i in range(steps):
# update the circuit parameters
params = opt.step(objective, params)
print('Cost after step {:5d}: {: .7f}'.format(i+1, objective(params)))
print('Optimized rotation angle: {}'.format(params))