amazon-braket / amazon-braket-sdk-python

A Python SDK for interacting with quantum devices on Amazon Braket
https://aws.amazon.com/braket/
Apache License 2.0
294 stars 118 forks source link

Is measurement on all qubits implied? #158

Closed we-taper closed 3 years ago

we-taper commented 3 years ago

Hi braket developers, I am looking at this document but I am confused by it. Since there is no measurements on qubits in the circuit, I do not expect to get any returns from running the circuit. But the code result seems to implied that all qubits are measured in the end of the experiment.

kshitijc commented 3 years ago

Hello! Yes, the default behavior (if no result types are specified) is to simply return the measurements over all qubits at the end of the experiment. The SDK also provides the ability to request specific result types on a subset of qubits. Please refer to the documentation here to see the available result types.

we-taper commented 3 years ago

@kshitijc Thanks for your reply. Shall we clarify this in the document as it is quite different from other quantum SDKs (e.g. qiskit/cirq)? Also, if specific result types are used, will it prevent measurements on qubits that do not have result types on them? For example, I am confused with the following simulation code:

# %% imports
import braket.circuits
import braket.devices
import braket.tasks.local_quantum_task
from braket.circuits.observables import X, Y

# %%
circuit = braket.circuits.Circuit()
circuit1 = circuit.h(0)
assert circuit1 is circuit
circuit.cnot(0, 1)
circuit.cnot(1, 2)
circuit.expectation(observable=X() @ Y(), target=[0, 1])
print(circuit.diagram())

# %% local simulator
sim = braket.devices.LocalSimulator()
nshots = 100
run_out = sim.run(circuit, shots=nshots)
run_result = run_out.result()
print('The measurement_counts hints that all qubits are measured.')
print(run_result.measurement_counts)
print('While the result_types hints that only a subset is measured')
print(run_result.result_types[0])

Output:

T  : |0|1|2|  Result Types  |

q0 : -H-C---Expectation(X@Y)-
        |   |                
q1 : ---X-C-Expectation(X@Y)-
          |                  
q2 : -----X------------------
T  : |0|1|2|  Result Types  |
The measurement_counts hints that all qubits are measured.
Counter({'011': 17, '101': 15, '001': 15, '100': 14, '111': 12, '110': 10, '000': 9, '010': 8})
While the result_types hints that only a subset is measured
type={'targets': [0, 1], 'type': 'expectation', 'observable': ['x', 'y']} value=-0.08
avawang1 commented 3 years ago

Thanks for reaching out.

If shots > 0, then all qubits are measured for the local simulator, cloud simulator SV1 and QPUs. The result type values are calculated from the measurements. Note that for result types requiring a different basis than Pauli Z, then additional basis rotation gates are applied to the qubits of the result type. The measurements are for all qubits after any basis rotation gates have been applied.

If shots == 0 for simulators, only the result types are returned.

we-taper commented 3 years ago

Thanks @avawang1 for this information. I am quite curious if this is due to hardware limitations? Because if it is a hardware limitation problem, then why should the simulators (local or SV1) also give back all qubit measurement results? Also, since the technology of partial measurement/non-demolition measurement is under development (I believe in some quantum architectures), may I expect to use this SDK to change when the technology is mature?

avawang1 commented 3 years ago

For shots > 0, we are treating SV1 and the local simulator as QPU-like, which means that result.measurements is available in addition to result.values if result types were specified. The list of qubits that were measured can be found in result.measured_qubits. For more documentation on the result, please see https://amazon-braket-sdk-python.readthedocs.io/en/latest/_apidoc/braket.tasks.gate_model_quantum_task_result.html.

If there are any changes to the SDK in the future, please be assured that we will announce them in the release notes.

we-taper commented 3 years ago

Excellent! Thanks for the explanations.