Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kshyatt-aws opened this issue Jul 9, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@kshyatt-aws
Copy link
Contributor

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:

  • Amazon Braket Python SDK version:
  • Amazon Braket Python Schemas version:
  • Amazon Braket Python Default Simulator version:
  • Python version:

Additional context
Add any other context about the problem here.

@kshyatt-aws kshyatt-aws added the bug Something isn't working label Jul 9, 2024
@speller26
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants