Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_Groq.py
Go to the documentation of this file.
1 from scipy.stats import unitary_group
2 import numpy as np
3 from squander import Variational_Quantum_Eigensolver
4 from squander import utils as utils
5 import time
6 import sys
7 import scipy as sp
8 import pytest
9 from networkx.generators.random_graphs import random_regular_graph
10 from qiskit.quantum_info import SparsePauliOp
11 np.random.seed(31415)
12 np.set_printoptions(linewidth=200)
13 
14 import pickle
15 from scipy.optimize import minimize
16 
17 # mpirun -n 2 --bind-to numa --map-by numa python ~/vqe_project/100_layers.py
18 # hwloc-bind --membind node:0 --cpubind node:0 -- python 100_layers.py
19 
20 try:
21  from mpi4py import MPI
22  MPI_imported = True
23 except ModuleNotFoundError:
24  MPI_imported = False
25 
26 #MPI_imported = False
27 
28 if MPI_imported:
29  comm = MPI.COMM_WORLD
30  rank = comm.Get_rank()
31  world_size = comm.Get_size()
32 
33 
34 
35 topology = []
36 
38 
39  topology = random_regular_graph(3,n,seed=31415).edges
40 
41  if MPI_imported:
42  topology = comm.bcast(topology, root=0)
43  '''
44 
45  topology = [[0,1],[0,4],[0,8],
46  [1,2],[1,5],
47  [2,3],[2,4],
48  [3,6],[3,9],
49  [4,5],
50  [5,6],
51  [6,7],
52  [7,8],[7,9],
53  [8,9]]
54 
55  '''
56  '''
57  topology = [[0,1],
58  [1,2],
59  [2,3],
60  [3,4],
61  [4,5],
62  [5,6],
63  [6,7],
64  [7,8],
65  [8,9]]
66  '''
67  oplist = []
68  for i in topology:
69  oplist.append(("XX",[i[0],i[1]],1))
70  oplist.append(("YY",[i[0],i[1]],1))
71  oplist.append(("ZZ",[i[0],i[1]],1))
72  for i in range(n):
73  oplist.append(("Z",[i],1))
74  return SparsePauliOp.from_sparse_list(oplist,num_qubits=n).to_matrix(True)
75 
76 
77 
79  topology = random_regular_graph(3,n,seed=31415).edges
80  oplist = []
81  for i in topology:
82  oplist.append(("XX",[i[0],i[1]],1))
83  oplist.append(("YY",[i[0],i[1]],1))
84  oplist.append(("ZZ",[i[0],i[1]],1))
85  for i in range(n):
86  oplist.append(("Z",[i],1))
87  return SparsePauliOp.from_sparse_list(oplist,num_qubits=n).to_matrix(True)
88 
89 layers = 500
90 inner_blocks = 1
91 
92 qbit_num = 20
93 
94 
95 
96 # generate the Hamiltonian
97 Hamiltonian = generate_hamiltonian_tmp( qbit_num )
98 project_name = 'Test_GROQ'
99 
100 with open(project_name + '_Hamiltonian.dat', 'wb') as file:
101  pickle.dump(Hamiltonian, file)
102  pickle.dump(topology, file)
103 
104 
105 # generate configuration dictionary for the solver
106 config = {"max_inner_iterations":800, #29000,
107  "batch_size": 100, #128,
108  "eta": 0.0001,
109  "adaptive_eta": 0,
110  "use_line_search": 0,
111  "output_periodicity": 1,
112  "convergence_length": 20}
113 
114 
115 
116 
117 # initiate the VQE object with the Hamiltonian
118 VQE_Heisenberg = Variational_Quantum_Eigensolver(Hamiltonian, qbit_num, config, accelerator_num=1)
119 
120 
121 # set the ansatz variant (U3 rotations and CNOT gates)
122 VQE_Heisenberg.set_Ansatz("HEA_ZYZ")
123 
124 # Set the name for the project
125 VQE_Heisenberg.set_Project_Name( project_name )
126 
127 # generate the circuit ansatz for the optimization
128 VQE_Heisenberg.Generate_Circuit( layers, inner_blocks)
129 
130 # create random initial parameters
131 param_num = VQE_Heisenberg.get_Parameter_Num()
132 print('The number of free parameters: ', param_num)
133 parameters = np.random.randn( param_num ) *2*np.pi
134 
135 if MPI_imported:
136  parameters = comm.bcast(parameters, root=0)
137 
138 
139 
140 start_time = time.time()
141 
142 cost_fnc_GROQ = VQE_Heisenberg.Optimization_Problem( parameters )
143 
144 print("--- %s seconds elapsed during cost function evaluation with Groq ---" % (time.time() - start_time))
145 
146 
147 
148 
149 
150 
151 # initiate the VQE object with the Hamiltonian
152 VQE_Heisenberg = Variational_Quantum_Eigensolver(Hamiltonian, qbit_num, config, accelerator_num=0)
153 
154 
155 # set the ansatz variant (U3 rotations and CNOT gates)
156 VQE_Heisenberg.set_Ansatz("HEA_ZYZ")
157 
158 # Set the name for the project
159 VQE_Heisenberg.set_Project_Name( project_name )
160 
161 # generate the circuit ansatz for the optimization
162 VQE_Heisenberg.Generate_Circuit( layers, inner_blocks)
163 
164 # create random initial parameters
165 param_num = VQE_Heisenberg.get_Parameter_Num()
166 print('The number of free parameters: ', param_num)
167 #parameters = np.random.randn( param_num ) *2*np.pi
168 
169 if MPI_imported:
170  parameters = comm.bcast(parameters, root=0)
171 
172 start_time = time.time()
173 
174 cost_fnc_CPU = VQE_Heisenberg.Optimization_Problem( parameters )
175 
176 print("--- %s seconds elapsed during cost function evaluation with CPU ---" % (time.time() - start_time))
177 
178 print( cost_fnc_GROQ, cost_fnc_CPU )
179 assert abs((cost_fnc_GROQ-cost_fnc_CPU)/cost_fnc_CPU) < 1e-3
180 
def generate_hamiltonian(n)
Definition: test_Groq.py:78
def generate_hamiltonian_tmp(n)
Definition: test_Groq.py:37