Skip to content

Commit

Permalink
Remove retrying dependency (#129)
Browse files Browse the repository at this point in the history
* Use Braket SDK for polling

* Use args directly

* Update version.py

* Extra fixes

* Update version.py

* Update .gitignore

* Update braket_backend.py

* Update braket_backend.py

* Update test_adapter.py

* Update braket_backend.py

* Update version.py

* Update test_adapter.py
  • Loading branch information
speller26 authored Nov 7, 2023
1 parent 49d0254 commit ec7d047
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ __pycache__

venv/
.idea/
*.iml
.vscode/
*.DS_Store

# Distribution / packaging
.Python
Expand Down
55 changes: 23 additions & 32 deletions qiskit_braket_provider/providers/braket_job.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""AWS Braket job."""
import os
from datetime import datetime
from typing import List, Optional, Union
from warnings import warn
Expand All @@ -11,20 +10,13 @@
from qiskit.providers import BackendV2, JobStatus, JobV1
from qiskit.result import Result
from qiskit.result.models import ExperimentResult, ExperimentResultData
from retrying import retry


def retry_if_result_none(result):
"""Retry on result function."""
return result is None


@retry(
retry_on_result=retry_if_result_none,
stop_max_delay=int(os.environ.get("QISKIT_BRAKET_PROVIDER_MAX_DELAY", 60000)),
wait_fixed=int(os.environ.get("QISKIT_BRAKET_PROVIDER_WAIT_TIME", 2000)),
wrap_exception=True,
)
def _get_result_from_aws_tasks(
tasks: Union[List[LocalQuantumTask], List[AwsQuantumTask]]
) -> Optional[List[ExperimentResult]]:
Expand All @@ -41,32 +33,31 @@ def _get_result_from_aws_tasks(

# For each task the results is get and filled into an ExperimentResult object
for task in tasks:
if task.state() in AwsQuantumTask.RESULTS_READY_STATES:
result: GateModelQuantumTaskResult = task.result()

if result.task_metadata.shots == 0:
statevector = result.values[
result._result_types_indices[
"{'type': <Type.statevector: 'statevector'>}"
]
result: GateModelQuantumTaskResult = task.result()
if not result:
return None

if result.task_metadata.shots == 0:
statevector = result.values[
result._result_types_indices[
"{'type': <Type.statevector: 'statevector'>}"
]
data = ExperimentResultData(
statevector=statevector,
)
else:
counts = {
k[::-1]: v for k, v in dict(result.measurement_counts).items()
} # convert to little-endian

data = ExperimentResultData(
counts=counts,
memory=[
"".join(shot_result[::-1].astype(str))
for shot_result in result.measurements
],
)
]
data = ExperimentResultData(
statevector=statevector,
)
else:
return None
counts = {
k[::-1]: v for k, v in dict(result.measurement_counts).items()
} # convert to little-endian

data = ExperimentResultData(
counts=counts,
memory=[
"".join(shot_result[::-1].astype(str))
for shot_result in result.measurements
],
)

experiment_result = ExperimentResult(
shots=result.task_metadata.shots,
Expand Down
2 changes: 1 addition & 1 deletion qiskit_braket_provider/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Qiskit-Braket provider version."""
__version__ = "0.0.4"
__version__ = "0.0.5"
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ certifi>=2021.5.30
qiskit-aer>=0.10.2
qiskit-terra>=0.19.2
amazon-braket-sdk>=1.56.0
retrying==1.3.3

setuptools>=40.1.0
numpy>=1.3
43 changes: 19 additions & 24 deletions tests/providers/test_adapter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Tests for Qiskti to Braket adapter."""
from unittest import TestCase
import pytest

from braket.circuits import Circuit, FreeParameter, observables
from braket.devices import LocalSimulator
Expand Down Expand Up @@ -201,32 +200,28 @@ def test_standard_gate_decomp(self):
parameter_bindings = dict(zip(parameters, parameter_values))
qiskit_circuit = qiskit_circuit.assign_parameters(parameter_bindings)

if standard_gate.name != "cu":
with self.subTest(f"Circuit with {standard_gate.name} gate."):
braket_job = backend.run(qiskit_circuit, shots=1000)
braket_result = braket_job.result().get_counts()
with self.subTest(f"Circuit with {standard_gate.name} gate."):
braket_job = backend.run(qiskit_circuit, shots=1000)
braket_result = braket_job.result().get_counts()

qiskit_job = execute(qiskit_circuit, aer_backend, shots=1000)
qiskit_result = qiskit_job.result().get_counts()
qiskit_job = execute(qiskit_circuit, aer_backend, shots=1000)
qiskit_result = qiskit_job.result().get_counts()

combined_results = combine_dicts(
{k: float(v) / 1000.0 for k, v in braket_result.items()},
qiskit_result,
)
combined_results = combine_dicts(
{k: float(v) / 1000.0 for k, v in braket_result.items()},
qiskit_result,
)

for key, values in combined_results.items():
percent_diff = abs(
((float(values[0]) - values[1]) / values[0]) * 100
)
abs_diff = abs(values[0] - values[1])
self.assertTrue(
percent_diff < 10 or abs_diff < 0.05,
f"Key {key} with percent difference {percent_diff} "
f"and absolute difference {abs_diff}. Original values {values}",
)
else:
with pytest.raises(TypeError):
convert_qiskit_to_braket_circuit(qiskit_circuit)
for key, values in combined_results.items():
percent_diff = abs(
((float(values[0]) - values[1]) / values[0]) * 100
)
abs_diff = abs(values[0] - values[1])
self.assertTrue(
percent_diff < 10 or abs_diff < 0.05,
f"Key {key} with percent difference {percent_diff} "
f"and absolute difference {abs_diff}. Original values {values}",
)

def test_exponential_gate_decomp(self):
"""Tests adapter translation of exponential gates"""
Expand Down

0 comments on commit ec7d047

Please sign in to comment.