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

Fix distributed circuit without gates execution #491

Merged
merged 3 commits into from
Oct 13, 2021
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
2 changes: 1 addition & 1 deletion src/qibo/core/distcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def execute(self, initial_state=None, nshots=None):

def get_initial_state(self, state=None):
""""""
if not self.queues.queues and self.queue:
if not self.queues.queues:
self.queues.set(self.queue)

if state is None:
Expand Down
8 changes: 3 additions & 5 deletions src/qibo/core/distutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,13 @@ def set(self, queue: List[gates.Gate]):
This method also creates the ``DistributedQubits`` object holding the
global qubits list.
"""
if not queue:
raise_error(RuntimeError, "No gates available to set for distributed run.")

counter = self.count(queue, self.nqubits)
if self.qubits is None:
self.qubits = DistributedQubits(counter.argsort()[:self.nglobal],
self.nqubits)
transformed_queue = self.transform(queue, counter)
self.create(transformed_queue)
if queue:
transformed_queue = self.transform(queue, counter)
self.create(transformed_queue)

def _ids(self, calc_devices: Dict[str, int]) -> Tuple[str, List[int]]:
"""Generator of device piece indices."""
Expand Down
5 changes: 0 additions & 5 deletions src/qibo/tests/test_core_distcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ def test_distributed_circuit_set_gates(backend):
for queues in c.queues.queues[0]:
assert len(queues) == 4

# Attempt to set gates before adding any gate
c = DistributedCircuit(6, devices)
with pytest.raises(RuntimeError):
c.queues.set(c.queue)


def test_distributed_circuit_set_gates_controlled(backend):
devices = {"/GPU:0": 2, "/GPU:1": 2}
Expand Down
34 changes: 26 additions & 8 deletions src/qibo/tests/test_core_distcircuit_execution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test functions defined in `qibo/core/distcircuit.py`."""
import pytest
import numpy as np
from qibo import gates
from qibo import K, gates
from qibo.models import Circuit
from qibo.core.distcircuit import DistributedCircuit
from qibo.tests.utils import random_state
Expand All @@ -22,7 +22,7 @@ def test_distributed_circuit_execution(backend, accelerators, use_global_qubits)
initial_state = random_state(c.nqubits)
final_state = dist_c(np.copy(initial_state))
target_state = c(np.copy(initial_state))
np.testing.assert_allclose(target_state, final_state)
K.assert_allclose(target_state, final_state)


def test_distributed_circuit_execution_pretransformed(backend, accelerators):
Expand All @@ -39,7 +39,7 @@ def test_distributed_circuit_execution_pretransformed(backend, accelerators):
initial_state = random_state(c.nqubits)
final_state = dist_c(np.copy(initial_state))
target_state = c(np.copy(initial_state))
np.testing.assert_allclose(target_state, final_state)
K.assert_allclose(target_state, final_state)


def test_distributed_circuit_execution_with_swap(backend, accelerators):
Expand All @@ -55,7 +55,7 @@ def test_distributed_circuit_execution_with_swap(backend, accelerators):
initial_state = random_state(c.nqubits)
final_state = dist_c(np.copy(initial_state))
target_state = c(np.copy(initial_state))
np.testing.assert_allclose(target_state, final_state)
K.assert_allclose(target_state, final_state)


def test_distributed_circuit_execution_special_gate(backend, accelerators):
Expand All @@ -67,7 +67,7 @@ def test_distributed_circuit_execution_special_gate(backend, accelerators):
c = Circuit(6)
c.add(gates.Flatten(np.copy(initial_state)))
c.add((gates.H(i) for i in range(dist_c.nlocal)))
np.testing.assert_allclose(dist_c(), c())
K.assert_allclose(dist_c(), c())


def test_distributed_circuit_execution_controlled_gate(backend, accelerators):
Expand All @@ -81,7 +81,7 @@ def test_distributed_circuit_execution_controlled_gate(backend, accelerators):
initial_state = random_state(c.nqubits)
final_state = dist_c(np.copy(initial_state))
target_state = c(np.copy(initial_state))
np.testing.assert_allclose(target_state, final_state)
K.assert_allclose(target_state, final_state)


def test_distributed_circuit_execution_controlled_by_gates(backend, accelerators):
Expand All @@ -102,7 +102,7 @@ def test_distributed_circuit_execution_controlled_by_gates(backend, accelerators
initial_state = random_state(c.nqubits)
final_state = dist_c(np.copy(initial_state))
target_state = c(np.copy(initial_state))
np.testing.assert_allclose(target_state, final_state)
K.assert_allclose(target_state, final_state)


def test_distributed_circuit_execution_addition(backend, accelerators):
Expand All @@ -124,4 +124,22 @@ def test_distributed_circuit_execution_addition(backend, accelerators):
c.add([gates.CNOT(i, i + 1) for i in range(5)])
c.add([gates.Z(i) for i in range(6)])
assert c.depth == dist_c.depth
np.testing.assert_allclose(dist_c(), c())
K.assert_allclose(dist_c(), c())


def test_distributed_circuit_empty_execution(backend, accelerators):
# test executing a circuit with the default initial state
c = DistributedCircuit(5, accelerators)
final_state = c().state()
target_state = np.zeros_like(final_state)
target_state[0] = 1
K.assert_allclose(final_state, target_state)
# test re-executing the circuit with a given initial state
initial_state = random_state(c.nqubits)
K.assert_allclose(c(initial_state), initial_state)
# test executing a new circuit with a given initial state
c = DistributedCircuit(5, accelerators)
initial_state = random_state(c.nqubits)
K.assert_allclose(c(initial_state), initial_state)
# test re-executing the circuit with the default initial state
K.assert_allclose(c(), target_state)