Skip to content

Commit

Permalink
Add supported result types to AwsQuantumSimulator and allow basis_rot…
Browse files Browse the repository at this point in the history
…ation_gates to be run twice on circuit (#101)
  • Loading branch information
avawang1 authored Jun 8, 2020
1 parent 05c4e31 commit 29bac7f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/braket/_sdk/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.4.0"
__version__ = "0.4.1"
3 changes: 2 additions & 1 deletion src/braket/aws/aws_quantum_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def refresh_metadata(self) -> None:
self._status = simulator_metadata.get("status")
self._status_reason = simulator_metadata.get("statusReason")
self._properties = {
k: simulator_metadata.get(k) for k in ["supportedQuantumOperations", "qubitCount"]
k: simulator_metadata.get(k)
for k in ["supportedQuantumOperations", "qubitCount", "supportedResultTypes"]
}

@property
Expand Down
10 changes: 5 additions & 5 deletions src/braket/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ def basis_rotation_instructions(self) -> List[Instruction]:
added_observables_targets = set()
for return_type in observable_return_types:
observable: Observable = return_type.observable
targets: List[List[int]] = [return_type.target] if return_type.target else [
QubitSet(qubit) for qubit in self._moments.qubits
targets: List[List[int]] = [list(return_type.target)] if return_type.target else [
list([qubit]) for qubit in self._moments.qubits
]

for target in targets:
Expand All @@ -166,15 +166,15 @@ def basis_rotation_instructions(self) -> List[Instruction]:
return basis_rotation_instructions

@staticmethod
def _observable_to_instruction(observable: Observable, targets: List[int]):
def _observable_to_instruction(observable: Observable, target_list: List[int]):
if isinstance(observable, TensorProduct):
instructions = []
for factor in observable.factors:
target = [targets.pop(0) for _ in range(factor.qubit_count)]
target = [target_list.pop(0) for _ in range(factor.qubit_count)]
instructions += Circuit._observable_to_instruction(factor, target)
return instructions
else:
return [Instruction(gate, targets) for gate in observable.basis_rotation_gates]
return [Instruction(gate, target_list) for gate in observable.basis_rotation_gates]

@property
def moments(self) -> Moments:
Expand Down
17 changes: 17 additions & 0 deletions test/unit_tests/braket/aws/common_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ class MockDevices:
"gateModelProperties": {
"qubitCount": 23,
"supportedQuantumOperations": ["CNOT", "H", "RZ", "RY", "RZ", "Toffoli"],
"supportedResultTypes": [
{
"name": "Sample",
"observables": ["X", "Y", "Z"],
"minShots": 1,
"maxShots": 100,
},
{"name": "Probability", "minShots": 1, "maxShots": 100,},
],
}
},
"name": "integ_test_simulator",
Expand All @@ -164,6 +173,14 @@ class MockDevices:
"Phase",
"CPhase",
],
"supportedResultTypes": [
{
"name": "Sample",
"observables": ["X", "Y", "Z"],
"minShots": 1,
"maxShots": 100,
}
],
}
},
"name": "integ_test_simulator",
Expand Down
6 changes: 6 additions & 0 deletions test/unit_tests/braket/aws/test_aws_quantum_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def test_simulator_refresh_metadata_success():
assert simulator.properties["supportedQuantumOperations"] == expected_metadata.get(
"supportedQuantumOperations"
)
assert simulator.properties["supportedResultTypes"] == expected_metadata.get(
"supportedResultTypes"
)
assert simulator.status == expected_metadata.get("status")
assert simulator.status_reason is None

Expand All @@ -63,6 +66,9 @@ def test_simulator_refresh_metadata_success():
assert simulator.properties["supportedQuantumOperations"] == expected_metadata.get(
"supportedQuantumOperations"
)
assert simulator.properties["supportedResultTypes"] == expected_metadata.get(
"supportedResultTypes"
)
assert simulator.status == expected_metadata.get("status")
assert simulator.status_reason == expected_metadata.get("statusReason")

Expand Down
14 changes: 14 additions & 0 deletions test/unit_tests/braket/circuits/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,20 @@ def test_basis_rotation_instructions_multiple_result_types_different_hermitian_t
assert circ.basis_rotation_instructions == expected


def test_basis_rotation_instructions_call_twice():
circ = (
Circuit()
.h(0)
.cnot(0, 1)
.expectation(observable=Observable.H() @ Observable.X(), target=[0, 1])
.sample(observable=Observable.H() @ Observable.X(), target=[0, 1])
.variance(observable=Observable.H() @ Observable.X(), target=[0, 1])
)
expected = [Instruction(Gate.Ry(-np.pi / 4), 0), Instruction(Gate.H(), 1)]
assert circ.basis_rotation_instructions == expected
assert circ.basis_rotation_instructions == expected


def test_depth_getter(h):
assert h.depth is h._moments.depth

Expand Down

0 comments on commit 29bac7f

Please sign in to comment.