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

EntanglementEntropy use circuits with fixed nqubits #448

Merged
merged 2 commits into from
Aug 2, 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
7 changes: 4 additions & 3 deletions src/qibo/core/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def density_matrix(self, x):
@abstract_callbacks.Callback.nqubits.setter
def nqubits(self, n: int):
from qibo import gates
if self._nqubits is not None and self._nqubits != n:
raise_error(RuntimeError,
f"Changing EntanglementEntropy nqubits from {self._nqubits} to {n}.")
self._nqubits = n
if self.partition is None:
self.partition = list(range(n // 2 + n % 2))
Expand Down Expand Up @@ -77,9 +80,7 @@ def set_nqubits(self, state):
if not isinstance(state, K.tensor_types):
raise_error(TypeError, "State of unknown type {} was given in callback "
"calculation.".format(type(state)))
if self._nqubits is None:
self.nqubits = int(math.log2(tuple(state.shape)[0]))

self.nqubits = int(math.log2(tuple(state.shape)[0]))

def state_vector_call(self, state):
self.set_nqubits(state)
Expand Down
5 changes: 5 additions & 0 deletions src/qibo/tests/test_core_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def target_entropy(t):
target = [0, target_entropy(0.1234), 0, target_entropy(0.4321)]
K.assert_allclose(entropy[:], target, atol=_atol)

c = Circuit(8, accelerators)
with pytest.raises(RuntimeError):
c.add(gates.CallbackGate(entropy))
state = c()


def test_entropy_large_circuit(backend, accelerators):
"""Check that entropy calculation works for variational like circuit."""
Expand Down