From d7fefa8e5516a7a4b63160c5da2baaebb050391e Mon Sep 17 00:00:00 2001 From: Matthew Harrigan Date: Tue, 5 Nov 2019 00:22:16 +0000 Subject: [PATCH] PointOptimizerSummary option to avoid flattening (#2461) This isn't actually enough as the point optimizer will flatten it (again). But this *does* make it so I can write my own PointOptimizer replacement that can respect it xref #2406 --- cirq/circuits/optimization_pass.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cirq/circuits/optimization_pass.py b/cirq/circuits/optimization_pass.py index 524a05c818b..d82d0791c72 100644 --- a/cirq/circuits/optimization_pass.py +++ b/cirq/circuits/optimization_pass.py @@ -30,8 +30,11 @@ class PointOptimizationSummary: """A description of a local optimization to perform.""" - def __init__(self, clear_span: int, clear_qubits: Iterable['cirq.Qid'], - new_operations: 'cirq.OP_TREE') -> None: + def __init__(self, + clear_span: int, + clear_qubits: Iterable['cirq.Qid'], + new_operations: 'cirq.OP_TREE', + preserve_moments: bool = False) -> None: """ Args: clear_span: Defines the range of moments to affect. Specifically, @@ -41,8 +44,17 @@ def __init__(self, clear_span: int, clear_qubits: Iterable['cirq.Qid'], with each affected moment. new_operations: The operations to replace the cleared out operations with. + preserve_moments: If set, `cirq.Moment` instances within + `new_operations` will be preserved exactly. Normally the + operations would be repacked to fit better into the + target space, which may move them between moments. + Please be advised that a PointOptimizer consuming this + summary will flatten operations no matter what, + see https://github.com/quantumlib/Cirq/issues/2406. """ - self.new_operations = tuple(ops.flatten_op_tree(new_operations)) + self.new_operations = tuple( + ops.flatten_op_tree(new_operations, + preserve_moments=preserve_moments)) self.clear_span = clear_span self.clear_qubits = tuple(clear_qubits)