Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.11k stars 2.34k forks source link

Results object should inform user if multiple results of same name #3207

Closed tboen1 closed 3 years ago

tboen1 commented 4 years ago

Information

What is the current behavior?

The device appears to be returning the same results for different circuits. 5 separate circuits were ran, and the exact same results were returned each time:

Screen Shot 2019-10-06 at 12 13 52 AM

Steps to reproduce the problem

A state vector was passed to the circuit initializer. The initializer was transpiled with varying optimization levels. When the optimized circuits were ran on the device, the exact same results were returned for each circuit. It seemed as if the device had simply run the first circuit once, and then duplicated the results again and again. It seems as if the other circuits were simply not run. An example is shown below:

init.initialize(state_vector, range(4)) circ = transpile(init, basis_gates = ['u3', 'cx'] opt0 = transpile(circ, backend=backend, seed_transpiler=11, optimization_level=0) opt1 = transpile(circ, backend=backend, seed_transpiler=11, optimization_level=1) opt2 = transpile(circ, backend=backend, seed_transpiler=11, optimization_level=2) opt3 = transpile(circ, backend=backend, seed_transpiler=11, optimization_level=3)

What is the expected behavior?

It would be expected for the circuits transpiled with higher optimization levels to have better performance. However, at the very least, the randomness of the quantum hardware should guarantee that each circuit have different results.

It is evident that different circuits are in fact being passed into the device,

Screen Shot 2019-10-06 at 12 15 45 AM

Suggested solutions

It may be that the device is cancelling redundant circuits.

kdk commented 4 years ago

Hi @tboen1 , thanks for reporting. Can you include the code you used to select a backend, run the circuits and fetch the results?

tboen1 commented 4 years ago

Thanks for the reply!

circuit_list = [circ, opt0, opt1, opt2, opt3]
device = 'ibmq_ourense'
shots = 8000
backend = qk.IBMQ.get_backend(device)
experiment = qk.execute(circuit_list, backend, shots=shots, optimization_level = 0)
result = experiment.result()
for i in range(len(circuit_list)):
    print(result.get_counts(circuit_list[i]))

Hope this helps, the circuits specified in circuit_list are shown in the original report

ajavadia commented 4 years ago

All of your circuits probably have the same name. Try checking circ.name, opt0.name, etc.

You can fix this by changing the last line to:

    print(result.get_counts(i))

We have to check in the results by name (it gets sent over wire and the QuantumCircuit instance may have been destroyed). The ability to look up by the circuit instance is merely a convenience. But I agree that this can be confusing, and the Result should warn you that multiple experiment results of same name are available.