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

AbstractCircuit.freeze should not reallocate moments #5878

Merged
merged 3 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,11 @@ def _load_contents_with_earliest_strategy(self, contents: 'cirq.OP_TREE'):
# each index.
for i in range(length):
if i in moments_by_index:
self._moments.append(moments_by_index[i].with_operations(op_lists_by_index[i]))
if op_lists_by_index[i]:
self._moments.append(moments_by_index[i].with_operations(op_lists_by_index[i]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we could modify Moment.with_operations instead, so that we can get this optimization in more scenarios, e.g.

    def with_operations(self, *contents: 'cirq.OP_TREE') -> 'cirq.Moment':
        ...
        flattened_contents = tuple(op_tree.flatten_to_ops(contents))
        if not flattened_contents:
            return self
        ...

Copy link
Collaborator Author

@vtomole vtomole Sep 19, 2022

Choose a reason for hiding this comment

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

Will revert this diff in the next commit,

else:
self._moments.append(moments_by_index[i])

else:
self._moments.append(Moment(op_lists_by_index[i]))

Expand Down
7 changes: 7 additions & 0 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4486,6 +4486,13 @@ def test_concat_ragged_alignment():
)


def test_freeze_not_relocate_moments():
q = cirq.q(0)
c = cirq.Circuit(cirq.X(q), cirq.measure(q))
f = c.freeze()
assert [mc is fc for mc, fc in zip(c, f)] == [True, True]


def test_factorize_one_factor():
circuit = cirq.Circuit()
q0, q1, q2 = cirq.LineQubit.range(3)
Expand Down