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

Feature/qi2 581/async execute handle #98

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/hqca_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def generate_circuit() -> str:
return circuit.content


async def execute(qi: QuantumInterface) -> None:
def execute(qi: QuantumInterface) -> None:
"""Run the classical part of the Hybrid Quantum/Classical Algorithm.

Args:
Expand All @@ -36,7 +36,7 @@ async def execute(qi: QuantumInterface) -> None:
"""
for i in range(1, 5):
circuit = generate_circuit()
result = await qi.execute_circuit(circuit, 1024)
result = qi.execute_circuit(circuit, 1024)

print(result.results)
print(result.shots_requested)
Expand Down
2 changes: 1 addition & 1 deletion quantuminspire/sdk/quantum_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, runtime: "LocalRuntime"):
self.runtime = runtime
self.results: List[Any] = []

async def execute_circuit(self, circuit: str, number_of_shots: int) -> ExecuteCircuitResult:
def execute_circuit(self, circuit: str, number_of_shots: int) -> ExecuteCircuitResult:
"""Execute a quantum circuit."""
results = self.runtime.run_quantum(circuit, number_of_shots)
self.results.append(results.results)
Expand Down
7 changes: 3 additions & 4 deletions quantuminspire/util/api/local_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
"""
import asyncio
import importlib
import sys
import types
Expand Down Expand Up @@ -63,14 +62,14 @@ def run(self, program: BaseAlgorithm, runtime_type_id: int) -> int:
"""Execute provided algorithm/circuit."""
if isinstance(program, HybridAlgorithm):
quantum_interface = QuantumInterface(self)
self._hybrid_results = asyncio.run(self.run_hybrid(program, quantum_interface))
self._hybrid_results = self.run_hybrid(program, quantum_interface)
return self.RunFakeID.HYBRID
if isinstance(program, Circuit):
self._quantum_results = self.run_quantum(program.content)
return self.RunFakeID.QUANTUM
raise AssertionError("Unknown algorithm type")

async def run_hybrid(self, algorithm: HybridAlgorithm, quantum_interface: QuantumInterfaceProtocol) -> Any:
def run_hybrid(self, algorithm: HybridAlgorithm, quantum_interface: QuantumInterfaceProtocol) -> Any:
"""Execute provided algorithm."""

# pylint: disable = E1101, W0122
Expand All @@ -83,7 +82,7 @@ async def run_hybrid(self, algorithm: HybridAlgorithm, quantum_interface: Quantu

if not hasattr(program, "execute") or not callable(program.execute):
raise AssertionError("'execute' hook not found or not callable.")
await program.execute(quantum_interface)
program.execute(quantum_interface)

if not hasattr(program, "finalize") or not callable(program.finalize):
raise AssertionError("'finalize' hook not found or not callable.")
Expand Down
2 changes: 1 addition & 1 deletion quantuminspire/util/api/quantum_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ class QuantumInterface(ABC):
results: List[Any]

@abstractmethod
async def execute_circuit(self, circuit: str, number_of_shots: int) -> ExecuteCircuitResult:
def execute_circuit(self, circuit: str, number_of_shots: int) -> ExecuteCircuitResult:
"""Execute a quantum circuit."""
raise NotImplementedError
8 changes: 4 additions & 4 deletions tests/util/api/test_local_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def quantum_interface() -> Mock:
def execute_circuit(_circuit: str, number_of_shots: int) -> ExecuteCircuitResult:
return ExecuteCircuitResult(results={}, shots_requested=number_of_shots, shots_done=number_of_shots)

quantum_interface.execute_circuit = AsyncMock(side_effect=execute_circuit)
quantum_interface.execute_circuit = Mock(side_effect=execute_circuit)
return quantum_interface


class MockLocalRuntime(LocalRuntime):
run_quantum = Mock(
spec=LocalRuntime.run_quantum, return_value=ExecuteCircuitResult(results={}, shots_requested=1, shots_done=1)
)
run_hybrid = AsyncMock(spec=LocalRuntime.run_hybrid, return_value={"test": "result"})
run_hybrid = Mock(spec=LocalRuntime.run_hybrid, return_value={"test": "result"})


@pytest.fixture
Expand All @@ -51,12 +51,12 @@ def test_local_runtime_run_quantum(qxelarator: Mock) -> None:
qxelarator.execute_string.assert_called_once()


async def test_local_runtime_run_hybrid(qxelarator: Mock, quantum_interface: Mock) -> None:
def test_local_runtime_run_hybrid(qxelarator: Mock, quantum_interface: Mock) -> None:
runtime = LocalRuntime(qxelarator)
file = Path("examples/hqca_circuit.py")
algorithm = HybridAlgorithm("test", "Test")
algorithm.read_file(file)
await runtime.run_hybrid(algorithm, quantum_interface)
runtime.run_hybrid(algorithm, quantum_interface)


def test_local_runtime_run_with_hybrid_algorithm(local_runtime: MockLocalRuntime) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/util/api/test_quantum_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ def run_quantum(_circuit: str, number_of_shots: int) -> ExecuteCircuitResult:

async def test_quantum_interface(local_runtime: Mock) -> None:
qi = QuantumInterface(local_runtime)
result = await qi.execute_circuit("circuit", 1)
result = qi.execute_circuit("circuit", 1)
assert result.shots_done == 1
local_runtime.run_quantum.assert_called_once()