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

Simplified _Queue append #1255

Merged
merged 6 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
44 changes: 23 additions & 21 deletions src/qibo/models/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class _Queue(list):
def __init__(self, nqubits):
super().__init__(self)
self.nqubits = nqubits
self.moments = [nqubits * [None]]
self.moment_index = nqubits * [0]
self.nmeasurements = 0

def to_fused(self):
"""Transform all gates in queue to :class:`qibo.gates.FusedGate`."""
Expand Down Expand Up @@ -88,24 +85,29 @@ def from_fused(self):
queue.append(gate.gates[0])
return queue

def append(self, gate: gates.Gate):
super().append(gate)
if gate.qubits:
qubits = gate.qubits
else: # special gate acting on all qubits
qubits = tuple(range(self.nqubits))

if isinstance(gate, gates.M):
self.nmeasurements += 1

# calculate moment index for this gate
idx = max(self.moment_index[q] for q in qubits)
for q in qubits:
if idx >= len(self.moments):
# Add a moment
self.moments.append(len(self.moments[-1]) * [None])
self.moments[idx][q] = gate
self.moment_index[q] = idx + 1
@property
def nmeasurements(self):
renatomello marked this conversation as resolved.
Show resolved Hide resolved
return len([1 for gate in self if isinstance(gate, gates.M)])
renatomello marked this conversation as resolved.
Show resolved Hide resolved

@property
def moments(self):
renatomello marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both properties are missing docstrings

moments = [self.nqubits * [None]]
moment_index = self.nqubits * [0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, users had access to moment_index and now they don't. I don't know if this is the intended behaviour or not.

for gate in self:
if not isinstance(gate, gates.CallbackGate):
qubits = gate.qubits
else: # special gate acting on all qubits
qubits = tuple(range(self.nqubits))
BrunoLiegiBastonLiegi marked this conversation as resolved.
Show resolved Hide resolved

# calculate moment index for this gate
idx = max(moment_index[q] for q in qubits)
for q in qubits:
if idx >= len(moments):
# Add a moment
moments.append(len(moments[-1]) * [None])
moments[idx][q] = gate
moment_index[q] = idx + 1
return moments


class Circuit:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_models_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ def test_parametrizedgates_class():


def test_queue_class():
from qibo.callbacks import EntanglementEntropy
from qibo.models.circuit import _Queue

entropy = EntanglementEntropy([0])
queue = _Queue(4)
gatelist = [
gates.H(0),
Expand All @@ -29,12 +31,14 @@ def test_queue_class():
gates.H(2),
gates.CNOT(1, 2),
gates.Y(3),
gates.CallbackGate(entropy),
]
for g in gatelist:
queue.append(g)
assert queue.moments == [
[gatelist[0], gatelist[1], gatelist[3], gatelist[5]],
[gatelist[2], gatelist[4], gatelist[4], None],
[gatelist[6] for _ in range(4)],
]


Expand Down
Loading