Skip to content
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
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/quimb/Contract-a-Grid-Circuit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@
"metadata": {},
"outputs": [],
"source": [
"ccq.MergeNQubitGates(n_qubits=2).optimize_circuit(compressed_c)\n",
"ccq.MergeNQubitGates(n_qubits=1).optimize_circuit(compressed_c)\n",
"compressed_c= cirq.merge_k_qubit_unitaries(compressed_c, k=2)\n",
"compressed_c = cirq.merge_k_qubit_unitaries(compressed_c, k=1)\n",
"\n",
"compressed_c = cirq.drop_negligible_operations(compressed_c, atol=1e-6)\n",
"compressed_c = cirq.drop_empty_moments(compressed_c)\n",
Expand Down
21 changes: 11 additions & 10 deletions cirq-core/cirq/contrib/quimb/grid_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def _interaction(
)


@cirq._compat.deprecated_class(deadline='v0.16', fix="Use cirq.merge_k_qubit_unitaries")
class MergeNQubitGates(cirq.PointOptimizer):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This class does more than cirq.merge_k_qubit_unitaries. It calls off to cirq.drop_negligible_operations and cirq.drop_empty_moments as well. Will existing tests for this optimizer pass if only cirq.merge_k_qubit_unitaries is called?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Can you point me to where MergeNQubitGates optimizer calls off to cirq.drop_negligible_operations and cirq.drop_empty_moments ?

Also, the call sites calling MergeNQubitGates have been updated to call cirq.merge_k_qubit_unitaries and no tests fail right now. But to answer your specific question, cirq.merge_k_qubit_unitaries does not call cirq.drop_negligible_operations and cirq.drop_empty_moments by default.

Copy link
Copy Markdown
Collaborator

@vtomole vtomole Jul 7, 2022

Choose a reason for hiding this comment

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

Whoops, i mistook this for simplify_expectation_value_circuit that's after this.

"""Optimizes runs of adjacent unitary n-qubit operations."""

Expand Down Expand Up @@ -131,14 +132,14 @@ def simplify_expectation_value_circuit(circuit_sand: cirq.Circuit):
things for you.
"""
n_op = sum(1 for _ in circuit_sand.all_operations())
circuit = circuit_sand.copy()
while True:
MergeNQubitGates(n_qubits=1).optimize_circuit(circuit_sand)
circuit_sand = cirq.drop_negligible_operations(circuit_sand, atol=1e-6)
MergeNQubitGates(n_qubits=2).optimize_circuit(circuit_sand)
circuit_sand = cirq.drop_empty_moments(circuit_sand)
new_n_op = sum(1 for _ in circuit_sand.all_operations())

if new_n_op < n_op:
n_op = new_n_op
else:
return
circuit = cirq.merge_k_qubit_unitaries(circuit, k=1)
circuit = cirq.drop_negligible_operations(circuit, atol=1e-6)
circuit = cirq.merge_k_qubit_unitaries(circuit, k=2)
circuit = cirq.drop_empty_moments(circuit)
new_n_op = sum(1 for _ in circuit.all_operations())
if new_n_op >= n_op:
break
n_op = new_n_op
circuit_sand._moments = circuit._moments