1
$\begingroup$

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.

$\endgroup$

1 Answer 1

1
$\begingroup$

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)
$\endgroup$
8
  • $\begingroup$ Does this split your circuits into multiple jobs, submit them, and consolidate the results if the number of circuits exceed the maximum allowed for a backend per job like IBMQJobManager.run does? quantumcomputing.stackexchange.com/a/12758 $\endgroup$
    – bisarch
    Oct 10, 2022 at 12:16
  • 1
    $\begingroup$ Yes! This happens in run_circuits() function which is used by QuantumInstance class. $\endgroup$ Oct 11, 2022 at 3:44
  • $\begingroup$ Is it possible to map the virtual qubits to particular physical qubits (something that I can do if I have access to the circuit) in the method above? For example quantumcomputing.stackexchange.com/questions/17736/… $\endgroup$
    – bisarch
    Oct 11, 2022 at 14:33
  • 1
    $\begingroup$ Note that, CircuitSampler takes an instance of QuantumInstance as a parameter (qi in your code snippet). Which means you can use the exact same idea. $\endgroup$ Oct 11, 2022 at 16:41
  • 1
    $\begingroup$ Instead of creating a new pass manager and struggling with its configuration, you can use one of the preset pass managers and change its layout_method: pmc = PassManagerConfig.from_backend(backend) pmc.layout_method = 'noise_adaptive' pass_manager = level_2_pass_manager(pmc) $\endgroup$ Oct 12, 2022 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.