QuTech-Delft / quantuminspire

Quantum Inspire SDK
Apache License 2.0
65 stars 27 forks source link

Wrong output results when using execute_qasm #118

Closed RobertWezemanTNO closed 2 years ago

RobertWezemanTNO commented 3 years ago

Bug report

What did you try to do?

When I execute circuits using execute_qasm, I get thrown an experiment optimization warning with the message that the experiment might take longer to execute. The obtained results are wrong. For example, when I execute the following code

from quantuminspire.credentials import get_authentication
from quantuminspire.api import QuantumInspireAPI

authentication = get_authentication()
QI_URL = r'https://api.quantum-inspire.com'

name = 'My-Test'
qi = QuantumInspireAPI(QI_URL, authentication, project_name=name)
backend = qi.get_backend_type_by_name('QX single-node simulator')

qasm = '''version 1.0

qubits 2

H q[0]
CNOT q[0], q[1]
H q[0]
'''

backend_type = qi.get_backend_type_by_name('QX single-node simulator')
result = qi.execute_qasm(qasm, backend_type=backend_type, number_of_shots=1)

print(result['histogram'])

The resulting print statement is

OrderedDict([('0', 1.0)])

Which is not the correct output for this simple circuit. When I look in My QI, I see the correct circuit with the wrong results. When I execute the circuit again from within MyQI everything seems to work just fine. When I execute the same code using Qiskit everything seems to work for me.

What did you expect to happen?

Output for the given example:

OrderedDict([('0', 0.25), ('1', 0.25), ('2', 0.25), ('3', 0.25),])

What actually happened?

Output for the given example:

OrderedDict([('0', 1.0)])

Environment

Please specify:

RobertWezemanTNO commented 3 years ago

It was pointed out to me that adding the flag full_state_projection=True will solve my problems.

...

result = qi.execute_qasm(qasm, backend_type=backend_type, number_of_shots=1, full_state_projection=True)
print(result['histogram'])
# OrderedDict([('0', 0.25), ('1', 0.25), ('2', 0.25), ('3', 0.25)])

Or with more shots

...

result = qi.execute_qasm(qasm, backend_type=backend_type, number_of_shots=1024, full_state_projection=True)
print(result['histogram'])
# OrderedDict([('1', 0.24609375), ('0', 0.2470703125), ('2', 0.23828125), ('3', 0.2685546875)])

It is however still unclear to me what the default setting full_state_projection=False is supposed to return and why it always returns OrderedDict([('0', 1.0)]).

Note: the default value for full_state_projection for API-calls execute_qasm and execute_qasm_async was changed in SDK Feature release 1.2.0 from True to False. See Release Notes 2020-05-15.

RobertWezemanTNO commented 3 years ago

The reason why execute_qasm(qasm, backend_type=backend_type, full_state_projection=False) always resulted in a null output was because the qasm-code did not contain measurements of qubits. In this case a null output is the same as a measurement of the zero state.

The following qasm-code yields the expected results.


qasm = '''version 1.0

qubits 2

H q[0]
CNOT q[0], q[1]
H q[0]

Measure q[0,1]
'''

This might need to be updated in the code-example in the README.

This issue can be closed.

QFer commented 3 years ago

@RobertWezemanTNO Hi Robert, I will update the example in the README. Thanks for the suggestion.