amazon-braket / amazon-braket-default-simulator-python

Quantum program simulators that can run locally
https://amazon-braket-default-simulator-python.readthedocs.io/
Apache License 2.0
67 stars 22 forks source link

LocalSimulator happily and incorrectly accepts result types on Sum observables #270

Open kshyatt-aws opened 4 months ago

kshyatt-aws commented 4 months ago

Describe the bug

>>> from braket.circuits import Circuit, Observable
>>> circ = Circuit().h(0).rx(1, 0.5).cnot(0, 1).ry(0, 0.5).expectation(Observable.X() @ Observable.X()+Observable.Z() @ Observable.Z(), target=[[0,1],[0,1]])  
>>> circ.result_types
[Expectation(observable=Sum(TensorProduct(X('qubit_count': 1), X('qubit_count': 1)), TensorProduct(Z('qubit_count': 1), Z('qubit_count': 1))), target=[QubitSet([Qubit(0), Qubit(1)]), QubitSet([Qubit(0), Qubit(1)])])] # Expectation of Sum observable
>>>
>>> from braket.devices import LocalSimulator
>>> device = LocalSimulator()
>>>
>>> task = device.run(circ, shots=0)
>>> task2 = device.run(circ, shots=10)
>>> task.result()
GateModelQuantumTaskResult(task_metadata=TaskMetadata(braketSchemaHeader=BraketSchemaHeader(name='braket.task_result.task_metadata', version='1'), id='f24c3c54-0826-49e3-95be-b78769af2879', shots=0, deviceId='braket_sv', deviceParameters=None, createdAt=None, endedAt=None, status=None, failureReason=None), additional_metadata=AdditionalMetadata(action=Program(braketSchemaHeader=BraketSchemaHeader(name='braket.ir.openqasm.program', version='1'), source='OPENQASM 3.0;\nqubit[2] q;\nh q[0];\nrx(0.5) q[1];\ncnot q[0], q[1];\nry(0.5) q[0];\n#pragma braket result expectation x(q[0]) @ x(q[1]) + z(q[0]) @ z(q[1])', inputs={}), dwaveMetadata=None, ionqMetadata=None, rigettiMetadata=None, oqcMetadata=None, xanaduMetadata=None, queraMetadata=None, simulatorMetadata=None, iqmMetadata=None), result_types=[ResultTypeValue(type=Expectation(observable=['x', 'x'], targets=[0, 1], type=<Type.expectation: 'expectation'>), value=0.8775825618903722)], values=[0.8775825618903722], measurements=None, measured_qubits=None, measurement_counts=None, measurement_probabilities=None, measurements_copied_from_device=None, measurement_counts_copied_from_device=None, measurement_probabilities_copied_from_device=None, _result_types_indices={"{'observable': ['x', 'x'], 'targets': [0, 1], 'type': <Type.expectation: 'expectation'>}": 0})
>>> task2.result()
GateModelQuantumTaskResult(task_metadata=TaskMetadata(braketSchemaHeader=BraketSchemaHeader(name='braket.task_result.task_metadata', version='1'), id='86fafa7e-1d50-499e-bd37-20c6bf40f1b7', shots=10, deviceId='braket_sv', deviceParameters=None, createdAt=None, endedAt=None, status=None, failureReason=None), additional_metadata=AdditionalMetadata(action=Program(braketSchemaHeader=BraketSchemaHeader(name='braket.ir.openqasm.program', version='1'), source='OPENQASM 3.0;\nqubit[2] q;\nh q[0];\nrx(0.5) q[1];\ncnot q[0], q[1];\nry(0.5) q[0];\n#pragma braket result expectation x(q[0]) @ x(q[1]) + z(q[0]) @ z(q[1])', inputs={}), dwaveMetadata=None, ionqMetadata=None, rigettiMetadata=None, oqcMetadata=None, xanaduMetadata=None, queraMetadata=None, simulatorMetadata=None, iqmMetadata=None), result_types=[ResultTypeValue(type=Expectation(observable=['x', 'x'], targets=[0, 1], type=<Type.expectation: 'expectation'>), value=0.8)], values=[0.8], measurements=array([[1, 1],
       [0, 0],
       [0, 0],
       [1, 1],
       [1, 1],
       [1, 1],
       [0, 0],
       [1, 1],
       [1, 0],
       [1, 1]]), measured_qubits=[0, 1], measurement_counts=Counter({'11': 6, '00': 3, '10': 1}), measurement_probabilities={'11': 0.6, '00': 0.3, '10': 0.1}, measurements_copied_from_device=True, measurement_counts_copied_from_device=False, measurement_probabilities_copied_from_device=False, _result_types_indices={"{'observable': ['x', 'x'], 'targets': [0, 1], 'type': <Type.expectation: 'expectation'>}": 0})

To reproduce A clear, step-by-step set of instructions to reproduce the bug.

Expected behavior The local simulator should reject any expectation, variance, or sample of a Sum observable from the BDK. What it definitely should not do is only calculate the expectation value of the first term and return that.

Screenshots or logs If applicable, add screenshots or logs to help explain your problem.

System information A description of your system. Please provide:

Additional context Add any other context about the problem here.

speller26 commented 3 weeks ago

Looks like a problem with the the OpenQASM interpreter; we'll have to generate a new parser that recognizes sum observables.