Hi @antalszava,
Thanks for your reply!
Following your advice, I upgraded PennyLane-Qiskit to version 0.16.0. Now my code works for qiskit simulator. Thanks for that! But the running time is still higher when I set parallel = True
. Then I run the code on the QVM device, as I remember you mentioned you’ve experienced better performance in particular with the QVM device. But I receive an error when I set parallel = True
which I could not understand… Here is the code:
def test(simulator,parallel):
dev = qml.device(simulator,device='4q-pyqvm')
t0 = time.time()
pars = np.random.rand(12)
qnodes = qml.QNodeCollection()
for i in range(12):
@qml.qnode(dev,interface='torch')
def circ(inputs,weights,i=i):
qml.RY(weights, wires=0)
qml.RY(inputs[i], wires=0)
return qml.expval(qml.PauliZ(0))
qnodes.append(circ)
res = qnodes(pars,0.1,parallel=parallel)
t1 = time.time()
print("Running time", (t1 - t0) )
test('forest.qvm',True)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-4-996259bf4fe9> in <module>
----> 1 test('forest.qvm',True)
<ipython-input-2-de01ad33e3df> in test(simulator, parallel)
12 qnodes.append(circ)
13
---> 14 res = qnodes(pars,0.1,parallel=parallel)
15 t1 = time.time()
16 print("Running time", (t1 - t0) )
~/anaconda3/lib/python3.7/site-packages/pennylane/collections/qnode_collection.py in __call__(self, *args, **kwargs)
274
275 def __call__(self, *args, **kwargs):
--> 276 results = self.evaluate(args, kwargs)
277 return self.convert_results(results, self.interface)
278
~/anaconda3/lib/python3.7/site-packages/pennylane/collections/qnode_collection.py in evaluate(self, args, kwargs)
227 results.append(dask.delayed(q)(*args, **kwargs))
228
--> 229 return dask.compute(*results, scheduler=_scheduler)
230
231 for q in self.qnodes:
~/anaconda3/lib/python3.7/site-packages/dask/base.py in compute(*args, **kwargs)
395 keys = [x.__dask_keys__() for x in collections]
396 postcomputes = [x.__dask_postcompute__() for x in collections]
--> 397 results = schedule(dsk, keys, **kwargs)
398 return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
399
~/anaconda3/lib/python3.7/site-packages/dask/threaded.py in get(dsk, result, cache, num_workers, **kwargs)
74 results = get_async(pool.apply_async, len(pool._pool), dsk, result,
75 cache=cache, get_id=_thread_get_id,
---> 76 pack_exception=pack_exception, **kwargs)
77
78 # Cleanup pools associated to dead threads
~/anaconda3/lib/python3.7/site-packages/dask/local.py in get_async(apply_async, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, **kwargs)
499 _execute_task(task, data) # Re-execute locally
500 else:
--> 501 raise_exception(exc, tb)
502 res, worker_id = loads(res_info)
503 state['cache'][key] = res
~/anaconda3/lib/python3.7/site-packages/dask/compatibility.py in reraise(exc, tb)
110 if exc.__traceback__ is not tb:
111 raise exc.with_traceback(tb)
--> 112 raise exc
113
114 else:
~/anaconda3/lib/python3.7/site-packages/dask/local.py in execute_task(key, task_info, dumps, loads, get_id, pack_exception)
270 try:
271 task, data = loads(task_info)
--> 272 result = _execute_task(task, data)
273 id = get_id()
274 result = dumps((result, id))
~/anaconda3/lib/python3.7/site-packages/dask/local.py in _execute_task(arg, cache, dsk)
251 func, args = arg[0], arg[1:]
252 args2 = [_execute_task(a, cache) for a in args]
--> 253 return func(*args2)
254 elif not ishashable(arg):
255 return arg
~/anaconda3/lib/python3.7/site-packages/pennylane/qnode.py in __call__(self, *args, **kwargs)
596
597 # execute the tape
--> 598 res = self.qtape.execute(device=self.device)
599
600 # if shots was changed
~/anaconda3/lib/python3.7/site-packages/pennylane/tape/tape.py in execute(self, device, params)
1321 params = self.get_parameters()
1322
-> 1323 return self._execute(params, device=device)
1324
1325 def execute_device(self, params, device):
~/anaconda3/lib/python3.7/site-packages/pennylane/interfaces/torch.py in _execute(self, params, **kwargs)
266 def _execute(self, params, **kwargs):
267 kwargs["tape"] = self
--> 268 res = _TorchInterface.apply(kwargs, *params)
269 return res
270
~/anaconda3/lib/python3.7/site-packages/pennylane/interfaces/torch.py in forward(ctx, input_kwargs, *input_)
71 # evaluate the tape
72 tape.set_parameters(ctx.all_params_unwrapped, trainable_only=False)
---> 73 res = tape.execute_device(ctx.args, device)
74 tape.set_parameters(ctx.all_params, trainable_only=False)
75
~/anaconda3/lib/python3.7/site-packages/pennylane/tape/tape.py in execute_device(self, params, device)
1352
1353 if isinstance(device, qml.QubitDevice):
-> 1354 res = device.execute(self)
1355 else:
1356 res = device.execute(self.operations, self.observables, {})
~/anaconda3/lib/python3.7/site-packages/pennylane_forest/qvm.py in execute(self, circuit, **kwargs)
156 self._circuit_hash = circuit.graph.hash
157
--> 158 return super().execute(circuit, **kwargs)
159
160 def apply(self, operations, **kwargs):
~/anaconda3/lib/python3.7/site-packages/pennylane/_qubit_device.py in execute(self, circuit, **kwargs)
194 # generate computational basis samples
195 if self.shots is not None or circuit.is_sampled:
--> 196 self._samples = self.generate_samples()
197
198 multiple_sampled_jobs = circuit.is_sampled and self._has_partitioned_shots()
~/anaconda3/lib/python3.7/site-packages/pennylane_forest/qvm.py in generate_samples(self)
230 def generate_samples(self):
231 if "pyqvm" in self.qc.name:
--> 232 return self.qc.run(self.prog, memory_map=self._parameter_map)
233
234 if self.circuit_hash is None:
~/anaconda3/lib/python3.7/site-packages/pyquil/api/_error_reporting.py in wrapper(*args, **kwargs)
249 global_error_context.log[key] = pre_entry
250
--> 251 val = func(*args, **kwargs)
252
253 # poke the return value of that call in
~/anaconda3/lib/python3.7/site-packages/pyquil/api/_quantum_computer.py in run(self, executable, memory_map)
136 for region_name, values_list in memory_map.items():
137 self.qam.write_memory(region_name=region_name, value=values_list)
--> 138 return self.qam.run().wait().read_memory(region_name="ro")
139
140 @_record_call
~/anaconda3/lib/python3.7/site-packages/pyquil/pyqvm.py in run(self)
265 for _ in range(self.program.num_shots):
266 self.wf_simulator.reset()
--> 267 self._execute_program()
268 for name in self.ram.keys():
269 self._memory_results.setdefault(name, list())
~/anaconda3/lib/python3.7/site-packages/pyquil/pyqvm.py in _execute_program(self)
482 halted = len(self.program) == 0
483 while not halted:
--> 484 halted = self.transition()
485
486 return self
~/anaconda3/lib/python3.7/site-packages/pyquil/pyqvm.py in transition(self)
311 """
312 assert self.program is not None
--> 313 instruction = self.program[self.program_counter]
314
315 if isinstance(instruction, Gate):
~/anaconda3/lib/python3.7/site-packages/pyquil/quil.py in __getitem__(self, index)
893 Program(self.instructions[index])
894 if isinstance(index, slice)
--> 895 else self.instructions[index]
896 )
897
IndexError: list index out of range