forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitial_state.py
More file actions
71 lines (59 loc) · 1.99 KB
/
initial_state.py
File metadata and controls
71 lines (59 loc) · 1.99 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
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
import cudaq
from cudaq import spin, Schedule, RungeKuttaIntegrator
import numpy as np
import matplotlib.pyplot as plt
import os
# On a system with multiple GPUs, `mpiexec` can be used as follows:
# `mpiexec -np <N> python3 multi_gpu.py `
cudaq.mpi.initialize()
# Set the target to our dynamics simulator
cudaq.set_target("dynamics")
# Large number of spins
N = 20
dimensions = {}
for i in range(N):
dimensions[i] = 2
# Observable is the average magnetization operator
avg_magnetization_op = spin.empty()
for i in range(N):
avg_magnetization_op += (spin.z(i) / N)
# Arbitrary coupling constant
g = 1.0
# Construct the Hamiltonian
H = spin.empty()
for i in range(N):
H += 2 * np.pi * spin.x(i)
H += 2 * np.pi * spin.y(i)
for i in range(N - 1):
H += 2 * np.pi * g * spin.x(i) * spin.x(i + 1)
H += 2 * np.pi * g * spin.y(i) * spin.z(i + 1)
steps = np.linspace(0.0, 1, 200)
schedule = Schedule(steps, ["time"])
# Initial state (expressed as an enum)
psi0 = cudaq.dynamics.InitialState.ZERO
# This can also be used to initialize a uniformly-distributed wave-function instead.
# `psi0 = cudaq.dynamics.InitialState.UNIFORM`
# Run the simulation
evolution_result = cudaq.evolve(H,
dimensions,
schedule,
psi0,
observables=[avg_magnetization_op],
collapse_operators=[],
store_intermediate_results=True,
integrator=RungeKuttaIntegrator())
exp_val = [
exp_vals[0].expectation()
for exp_vals in evolution_result.expectation_values()
]
if cudaq.mpi.rank() == 0:
# Plot the results
fig = plt.figure(figsize=(12, 6))
plt.plot(steps, exp_val)
plt.ylabel("Average Magnetization")
plt.xlabel("Time")
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
fig.savefig("spin_model.png", dpi=fig.dpi)
cudaq.mpi.finalize()