-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
100 lines (72 loc) · 2.18 KB
/
example.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import numpy as np
import functools
import itertools
import matplotlib.pyplot as plt
from scipy import linalg as LA
import json
from qiskit import IBMQ, Aer
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit import ParameterVector
from qiskit.utils import QuantumInstance
from qiskit.opflow import Z, I, X, Y
from qiskit.opflow import PauliOp, SummedOp
from pauli_function import *
from pVQD import *
from ansatze import *
# Initialize system parameters for Ising
spins = 3
V = 0.25
g = 1.0
dt = 0.05
n_steps = 40
# Algorithm parameters
ths = 0.99999
depth = 3
### Example circ
ex_params = np.zeros((depth+1)*spins +depth*(spins-1))
wfn = hweff_ansatz(spins,depth,ex_params)
### Shift
shift = np.array(len(ex_params)*[0.01])
print("Initial shift:",shift)
### Generate the Hamiltonian
H = generate_ising(spins,V,g)
print(wfn)
print(H)
### Backend
shots = 8000
backend = Aer.get_backend('qasm_simulator')
instance = QuantumInstance(backend=backend,shots=shots)
### Prepare the observables to measure
obs = {}
# Magnetization
for i in range(spins):
obs['Sz_'+str(i)] = PauliOp(generate_pauli([],[i],spins),1.0)
obs['Sx_'+str(i)] = PauliOp(generate_pauli([i],[],spins),1.0)
obs['Sy_'+str(i)] = PauliOp(generate_pauli([i],[i],spins),1.0)
for (name,pauli) in obs.items():
print(name)
print(pauli)
### Initialize the algorithm
# Choose a specific set of parameters
initial_point = None
# Choose the gradient optimizer: 'sgd', 'adam'
opt = 'sgd'
# Choose how to estimate the gradient on hardware: 'param_shift', 'spsa'
grad = 'param_shift'
# Choose which type of cost function use: 'global', 'local'
cost = 'local'
algo = pVQD(hamiltonian = H,
ansatz = hweff_ansatz,
ansatz_reps = depth,
parameters = ex_params,
initial_shift = shift,
instance = instance,
shots = shots)
algo.run(ths,dt,n_steps,
obs_dict = obs,
filename = 'data/trial_results.dat',
max_iter = 50,
opt = opt,
cost_fun = cost,
grad = grad,
initial_point = initial_point)