@@ -10,10 +10,11 @@ class QAOA:
1010 cost_hamiltonian : str
1111 layer_circuit : str
1212 use_subroutines : bool
13+ use_input : bool
1314
14- def __init__ (self , num_qubits : int , use_subroutines : bool = False , qasm_version : int = 3 ):
15+ def __init__ (self , num_qubits : int , qasm_version : int = 3 , use_input : bool = True ):
1516 self .builder = QasmBuilder (num_qubits , version = qasm_version )
16- self .builder . claim_clbits ( num_qubits )
17+ self .use_input = use_input
1718
1819
1920 def xy_mixer (self , graph : nx .Graph ) -> str :
@@ -38,13 +39,15 @@ def xy_mixer(self, graph : nx.Graph) -> str:
3839 std .begin_subroutine (
3940 mixer_name , [qubit_array_param , alpha ]
4041 )
42+ old_call_space = std .call_space
43+ std .call_space = "qubits[{}]"
4144
4245 for i ,j in graph .edges :
4346 std .cnot (i ,j )
4447 std .rx ("-alpha" , j )
4548 std .ry ("-alpha" , j )
4649 std .cnot (i ,j )
47-
50+ std . call_space = old_call_space
4851 std .end_subroutine ()
4952
5053 return mixer_name
@@ -71,10 +74,11 @@ def x_mixer(self, graph : nx.Graph) -> str:
7174 std .begin_subroutine (
7275 mixer_name , [qubit_array_param , alpha ]
7376 )
74-
75- for i in range (self .builder .qubits ):
76- std .rx ("-2 * alpha" , i )
77-
77+ old_call_space = std .call_space
78+ std .call_space = "qubits[{}]"
79+ for i in graph .nodes :
80+ std .rx ("2 * alpha" , i )
81+ std .call_space = old_call_space
7882 std .end_subroutine ()
7983
8084 return mixer_name
@@ -102,7 +106,8 @@ def min_vertex_cover_cost(self, graph : nx.Graph) -> str:
102106 std .begin_subroutine (
103107 cost_name , [qubit_array_param , gamma ]
104108 )
105-
109+ old_call_space = std .call_space
110+ std .call_space = "qubits[{}]"
106111 for i ,j in graph .edges :
107112 std .cnot (i ,j )
108113 std .rz ("3 * 2 * gamma" , j )
@@ -112,7 +117,7 @@ def min_vertex_cover_cost(self, graph : nx.Graph) -> str:
112117
113118 for i in graph .nodes :
114119 std .rz ("-2 * gamma" , i )
115-
120+ std . call_space = old_call_space
116121 std .end_subroutine ()
117122
118123
@@ -141,7 +146,8 @@ def max_clique_cost(self, graph : nx.Graph) -> str:
141146 std .begin_subroutine (
142147 cost_name , [qubit_array_param , gamma ]
143148 )
144-
149+ old_call_space = std .call_space
150+ std .call_space = "qubits[{}]"
145151 graph_complement = nx .complement (graph )
146152
147153 for i ,j in graph_complement .edges :
@@ -153,7 +159,7 @@ def max_clique_cost(self, graph : nx.Graph) -> str:
153159
154160 for i in graph .nodes :
155161 std .rz ("2 * gamma" , i )
156-
162+ std . call_space = old_call_space
157163 std .end_subroutine ()
158164
159165
@@ -184,12 +190,13 @@ def qaoa_maxcut(self, graph : nx.Graph) -> tuple[str, str] :
184190 std .begin_subroutine (
185191 cost_name , [qubit_array_param , gamma ]
186192 )
187-
193+ old_call_space = std .call_space
194+ std .call_space = "qubits[{}]"
188195 for i ,j in graph .edges :
189196 std .cnot (i ,j )
190- std .rz ("2 * gamma" , j )
197+ std .rz ("- 2 * gamma" , j )
191198 std .cnot (i ,j )
192-
199+ std . call_space = old_call_space
193200 std .end_subroutine ()
194201
195202
@@ -239,7 +246,7 @@ def layer(self, cost_ham : str, mixer_ham : str) -> str :
239246
240247 return name
241248
242- def generate_algorithm (self , cost_ham : str , depth : int , layer : str = "" , epsilon : float = 0.01 ) -> str :
249+ def generate_algorithm (self , depth : int , layer : str = "" , param : list [ float ] = [] ) -> str :
243250 """
244251 Load the Quantum Approximate Optimization Algorithm (QAOA) ansatz as a pyqasm module.
245252
@@ -261,18 +268,16 @@ def generate_algorithm(self, cost_ham : str, depth : int, layer : str = "", epsi
261268 layer = self .layer_circuit if layer == "" else layer
262269
263270 num_qubits = self .builder .qubits
264- self .builder .claim_qubits (self .builder .qubits )
265- self .builder .claim_qubits (1 )
266-
267- repetitions = int (round ((1.0 / epsilon )** 2 ))
271+ #self.builder.claim_qubits(self.builder.qubits)
272+ #self.builder.claim_qubits(1)
268273
269274 for i in range (depth ):
270- std . add_input_var ( f"gamma_ { i } " , qtype = "float" )
271- std .add_input_var (f"alpha_ { i } " , qtype = "float" )
272-
273- std . add_var ( name = "measure_0" , qtype = "int" )
274- std .add_output_var ( "expval" , qtype = "float " )
275- std .begin_loop ( repetitions )
275+ if self . use_input :
276+ std .add_input_var (f"gamma_ { i } " , qtype = "float" )
277+ std . add_input_var ( f"alpha_ { i } " , qtype = "float" )
278+ else :
279+ std .classical_op ( f"float gamma_ { i } = { param [ i ] } " )
280+ std .classical_op ( f"float alpha_ { i } = { param [ i + 1 ] } " )
276281
277282 for q in range (self .builder .qubits ):
278283 std .reset (q )
@@ -282,24 +287,22 @@ def generate_algorithm(self, cost_ham : str, depth : int, layer : str = "", epsi
282287
283288 for i in range (depth ):
284289 std .call_subroutine (layer , parameters = [f"qb[0:{ num_qubits } ]" , f"gamma_{ i } " , f"alpha_{ i } " ])
285- std .call_subroutine (layer , parameters = [f"qb[{ num_qubits } :{ num_qubits * 2 } ]" , f"gamma_{ i } " , f"alpha_{ i } " ])
286290
287- std .call_subroutine (cost_ham , [f"qb[0:{ num_qubits } ]" , "1" ])
288- std .h (self .builder .qubits - 1 )
289- for q in range (num_qubits ):
291+ #std.call_subroutine(cost_ham, [f"qb[0:{num_qubits}]", "1"])
292+ #std.h(self.builder.qubits - 1)
293+ std .measure (list (range (num_qubits )), list (range (num_qubits )))
294+ """for q in range(num_qubits):
290295 std.cswap(control=f"qb[{self.builder.qubits - 1}]", targ1=f"qb[{q}]", targ2=f"qb[{q+num_qubits}]")
291296 std.h(self.builder.qubits - 1)
292297 std.measure([self.builder.qubits - 1], [0])
293298
294299 std.begin_if("cb[0] == 0")
295300 std.classical_op("measure_0 = measure_0 + 1")
296- std .end_if ()
297-
298- std .end_loop ()
301+ std.end_if()"""
299302
300- std .classical_op (f"expval = measure_0/{ repetitions } " )
303+ """ std.classical_op(f"expval = measure_0/{repetitions}")
301304 std.classical_op("expval = 2*(expval - 0.5)")
302305 std.classical_op("expval = sqrt(expval)")
303- std .classical_op ("expval = log(expval)" )
306+ std.classical_op("expval = log(expval)")"""
304307
305308 return self .builder .build ()
0 commit comments