1

I need to calculate the expectation values of an operator h for various quantum states ψ on an IBMQ quantum machine. This can be done using the method described in this answer like the following,

 for i, inp in enumerate(x):
     parameters = {list(a.parameters)[0]: inp}
     ψ = CircuitStateFn(a.assign_parameters(parameters))
     measurable_expression = StateFn(h, is_measurement=True).compose(ψ)   
     expectation = PauliExpectation().convert(measurable_expression)
     sampler = CircuitSampler(qi).convert(expectation)
     y[i] = sampler.eval().real 
     z[i] = sampler.eval().imag

Now, I've noticed that these jobs sits idle in the queue for a long time before being executed which itself doesn't take too long. So I am thinking of submitting multiple jobs in parallel. I thought of providing a ThreadPoolExecutor to the backend but it seems that it only works with AerSimulator not real quantum hardware.

Is there a way to do this? Thanks.

bisarch
  • 199
  • 1
  • 7

1 Answers1

1

You can add all the measurement expressions in one list operator. This way, all the circuits needed to be executed will be sent to the backend at once as a single job:

from qiskit.opflow.list_ops import ListOp

measurable_expressions_list = [] for i, inp in enumerate(x): parameters = {list(a.parameters)[0]: inp} ψ = CircuitStateFn(a.assign_parameters(parameters)) measurable_expression = StateFn(h, is_measurement=True).compose(ψ) measurable_expressions_list.append(measurable_expression)

expect_ops = PauliExpectation().convert(ListOp(measurable_expressions_list)) sampler = CircuitSampler(quantum_instance) sampled_ops = sampler.convert(expect_ops) expectation_values = sampled_ops.eval()

for i, exp_val in enumerate(expectation_values): y[i] = exp_val.real z[i] = exp_val.imag

print(y, z)

Egretta.Thula
  • 11,986
  • 1
  • 13
  • 34