Skip to content

Commit

Permalink
bugfix: Handle meas_level from backend.run (#142)
Browse files Browse the repository at this point in the history
* Validate meas_level if it is supplied to run, and if it's valid, remove it. AwsQuantumTask does not accept meas_level argument
  • Loading branch information
laurencap authored Feb 2, 2024
1 parent 5664916 commit daf9e9d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
17 changes: 17 additions & 0 deletions qiskit_braket_provider/providers/braket_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import datetime
import logging
import enum
from abc import ABC
from typing import Iterable, Union, List

Expand Down Expand Up @@ -36,6 +37,15 @@ class BraketBackend(BackendV2, ABC):
def __repr__(self):
return f"BraketBackend[{self.name}]"

def _validate_meas_level(self, meas_level: Union[enum.Enum, int]):
if isinstance(meas_level, enum.Enum):
meas_level = meas_level.value
if meas_level != 2:
raise QiskitBraketException(
f"Device {self.name} only supports classified measurement "
f"results, received meas_level={meas_level}."
)


class BraketLocalBackend(BraketBackend):
"""BraketLocalBackend."""
Expand Down Expand Up @@ -109,6 +119,9 @@ def run(
shots = options["shots"] if "shots" in options else 1024
if shots == 0:
circuits = list(map(lambda x: x.state_vector(), circuits))
if "meas_level" in options:
self._validate_meas_level(options["meas_level"])
del options["meas_level"]
tasks = []
try:
for circuit in circuits:
Expand Down Expand Up @@ -278,6 +291,10 @@ def run(self, run_input, **options):
else:
raise QiskitBraketException(f"Unsupported input type: {type(run_input)}")

if "meas_level" in options:
self._validate_meas_level(options["meas_level"])
del options["meas_level"]

braket_circuits = list(convert_qiskit_to_braket_circuits(circuits))

if options.pop("verbatim", False):
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

from qiskit import (
QuantumCircuit,
BasicAer,
QuantumRegister,
ClassicalRegister,
transpile,
)
from qiskit.providers.basicaer import BasicAer
from qiskit.circuit import Parameter
from qiskit.circuit.library import PauliEvolutionGate
from qiskit.quantum_info import SparsePauliOp
Expand Down
22 changes: 20 additions & 2 deletions tests/providers/test_braket_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from botocore import errorfactory
from braket.aws.queue_information import QueueDepthInfo, QueueType
from qiskit import QuantumCircuit, transpile, BasicAer
from qiskit import QuantumCircuit, transpile
from qiskit.providers.basicaer import BasicAer

from qiskit.algorithms.minimum_eigensolvers import VQE, VQEResult

Expand All @@ -20,7 +21,7 @@
from qiskit.transpiler import Target
from qiskit.primitives import BackendEstimator

from qiskit_braket_provider import AWSBraketProvider, version
from qiskit_braket_provider import AWSBraketProvider, version, exception
from qiskit_braket_provider.providers import AWSBraketBackend, BraketLocalBackend
from qiskit_braket_provider.providers.adapter import aws_device_to_target
from tests.providers.mocks import (
Expand Down Expand Up @@ -141,6 +142,23 @@ def test_local_backend_circuit_shots0(self):
self.assertEqual(statevector[2], 0.0 + 0.0j)
self.assertEqual(statevector[3], 1.0 + 0.0j)

def test_meas_level_2(self):
"""Check that there's no error for asking for classified measurement results."""
backend = BraketLocalBackend(name="default")
circuit = QuantumCircuit(1, 1)
circuit.h(0)
circuit.measure(0, 0)
backend.run(circuit, shots=10, meas_level=2)

def test_meas_level_1(self):
"""Check that there's an exception for asking for raw measurement results."""
backend = BraketLocalBackend(name="default")
circuit = QuantumCircuit(1, 1)
circuit.h(0)
circuit.measure(0, 0)
with self.assertRaises(exception.QiskitBraketException):
backend.run(circuit, shots=10, meas_level=1)

def test_vqe(self):
"""Tests VQE."""
local_simulator = BraketLocalBackend(name="default")
Expand Down

0 comments on commit daf9e9d

Please sign in to comment.