def assert_optimizes( before: cirq.Circuit, expected: cirq.Circuit, pre_opts: Iterable[cirq.OptimizationPass] = (cg.ConvertToXmonGates( ignore_failures=True), ), post_opts: Iterable[cirq.OptimizationPass] = (cg.ConvertToXmonGates( ignore_failures=True), cirq.DropEmptyMoments())): opt = cg.EjectZ() circuit = before.copy() for pre in pre_opts: pre.optimize_circuit(circuit) opt.optimize_circuit(circuit) for post in post_opts: post.optimize_circuit(circuit) post.optimize_circuit(expected) if circuit != expected: # coverage: ignore print("BEFORE") print(before) print("AFTER") print(circuit) print("EXPECTED") print(expected) assert circuit == expected # And it should be idempotent. opt.optimize_circuit(circuit) assert circuit == expected
def assert_optimizes( before: cirq.Circuit, expected: cirq.Circuit, pre_opts: Iterable[cirq.OptimizationPass] = (cg.ConvertToXmonGates( ignore_failures=True), ), post_opts: Iterable[cirq.OptimizationPass] = (cg.ConvertToXmonGates( ignore_failures=True), cirq.DropEmptyMoments())): opt = cg.EjectZ() if cirq.has_unitary(before): cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent( before, expected, atol=1e-8) circuit = before.copy() for pre in pre_opts: pre.optimize_circuit(circuit) opt.optimize_circuit(circuit) for post in post_opts: post.optimize_circuit(circuit) post.optimize_circuit(expected) cirq.testing.assert_same_circuits(circuit, expected) # And it should be idempotent. opt.optimize_circuit(circuit) cirq.testing.assert_same_circuits(circuit, expected)
def assert_removes_all_z_gates(circuit: cirq.Circuit): opt = cg.EjectZ() optimized = circuit.copy() opt.optimize_circuit(optimized) has_z = any( _try_get_known_z_half_turns(op) is not None for moment in optimized for op in moment.operations) assert not has_z cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent( circuit, optimized, atol=1e-8)
def assert_optimizes(before: cirq.Circuit, expected: cirq.Circuit): actual = cirq.Circuit(before) opt = cirq.MergeInteractions() opt.optimize_circuit(actual) # Ignore differences that would be caught by follow-up optimizations. followup_optimizations = [ cg.MergeRotations(), cg.EjectFullW(), cg.EjectZ(), cirq.DropNegligible(), cirq.DropEmptyMoments() ] for post in followup_optimizations: post.optimize_circuit(actual) post.optimize_circuit(expected) if actual != expected: # coverage: ignore print('ACTUAL') print(actual) print('EXPECTED') print(expected) assert actual == expected