def test_align_left_subset_of_operations(): q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') tag = "op_to_align" c_orig = cirq.Circuit([ cirq.Moment([cirq.Y(q1)]), cirq.Moment([cirq.X(q2)]), cirq.Moment([cirq.X(q1).with_tags(tag)]), cirq.Moment([cirq.Y(q2)]), cirq.measure(*[q1, q2], key='a'), ]) c_exp = cirq.Circuit([ cirq.Moment([cirq.Y(q1)]), cirq.Moment([cirq.X(q1).with_tags(tag), cirq.X(q2)]), cirq.Moment(), cirq.Moment([cirq.Y(q2)]), cirq.measure(*[q1, q2], key='a'), ]) cirq.testing.assert_same_circuits( cirq.toggle_tags( cirq.align_left( cirq.toggle_tags(c_orig, [tag]), context=cirq.TransformerContext(tags_to_ignore=[tag]), ), [tag], ), c_exp, )
def test_align_left_deep(): q1, q2 = cirq.LineQubit.range(2) c_nested = cirq.FrozenCircuit( [ cirq.Moment([cirq.X(q1)]), cirq.Moment([cirq.Y(q2)]), cirq.Moment([cirq.Z(q1), cirq.Y(q2).with_tags("nocompile")]), cirq.Moment([cirq.Y(q1)]), cirq.measure(q2, key='a'), cirq.Z(q1).with_classical_controls('a'), ] ) c_nested_aligned = cirq.FrozenCircuit( cirq.Moment(cirq.X(q1), cirq.Y(q2)), cirq.Moment(cirq.Z(q1)), cirq.Moment([cirq.Y(q1), cirq.Y(q2).with_tags("nocompile")]), cirq.measure(q2, key='a'), cirq.Z(q1).with_classical_controls('a'), ) c_orig = cirq.Circuit( c_nested, cirq.CircuitOperation(c_nested).repeat(6).with_tags("nocompile"), c_nested, cirq.CircuitOperation(c_nested).repeat(5).with_tags("preserve_tag"), ) c_expected = cirq.Circuit( c_nested_aligned, cirq.CircuitOperation(c_nested).repeat(6).with_tags("nocompile"), c_nested_aligned, cirq.CircuitOperation(c_nested_aligned).repeat(5).with_tags("preserve_tag"), ) context = cirq.TransformerContext(tags_to_ignore=["nocompile"], deep=True) cirq.testing.assert_same_circuits(cirq.align_left(c_orig, context=context), c_expected)
def test_align_left_no_compile_context(): q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') cirq.testing.assert_same_circuits( cirq.align_left( cirq.Circuit( [ cirq.Moment([cirq.X(q1)]), cirq.Moment([cirq.Y(q1), cirq.X(q2)]), cirq.Moment([cirq.X(q1), cirq.Y(q2).with_tags("nocompile")]), cirq.Moment([cirq.Y(q1)]), cirq.measure(*[q1, q2], key='a'), ] ), context=cirq.TransformerContext(tags_to_ignore=["nocompile"]), ), cirq.Circuit( [ cirq.Moment([cirq.X(q1), cirq.X(q2)]), cirq.Moment([cirq.Y(q1)]), cirq.Moment([cirq.X(q1), cirq.Y(q2).with_tags("nocompile")]), cirq.Moment([cirq.Y(q1)]), cirq.measure(*[q1, q2], key='a'), ] ), )
def test_classical_control(): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit( cirq.H(q0), cirq.measure(q0, key='m'), cirq.X(q1).with_classical_controls('m') ) cirq.testing.assert_same_circuits(cirq.align_left(circuit), circuit) cirq.testing.assert_same_circuits(cirq.align_right(circuit), circuit)
def test_align_basic_no_context(): q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') c = cirq.Circuit( [ cirq.Moment([cirq.X(q1)]), cirq.Moment([cirq.Y(q1), cirq.X(q2)]), cirq.Moment([cirq.X(q1)]), ] ) cirq.testing.assert_same_circuits( cirq.align_left(c), cirq.Circuit( cirq.Moment([cirq.X(q1), cirq.X(q2)]), cirq.Moment([cirq.Y(q1)]), cirq.Moment([cirq.X(q1)]), ), ) cirq.testing.assert_same_circuits( cirq.align_right(c), cirq.Circuit( cirq.Moment([cirq.X(q1)]), cirq.Moment([cirq.Y(q1)]), cirq.Moment([cirq.X(q1), cirq.X(q2)]), ), )
def test_merge_operations_does_not_merge_measurements_behind_ccos(): q = cirq.LineQubit.range(2) measure_op = cirq.measure(q[0], key="a") cco_op = cirq.X(q[1]).with_classical_controls("a") def merge_func(op1, op2): return cirq.I( *op1.qubits) if op1 == measure_op and op2 == measure_op else None circuit = cirq.Circuit([cirq.H(q[0]), measure_op, cco_op] * 2) cirq.testing.assert_same_circuits( cirq.merge_operations(circuit, merge_func), circuit) circuit = cirq.Circuit( [cirq.H(q[0]), measure_op, cco_op, measure_op, measure_op] * 2) expected_circuit = cirq.Circuit( [cirq.H(q[0]), measure_op, cco_op, cirq.I(q[0])] * 2) cirq.testing.assert_same_circuits( cirq.align_left(cirq.merge_operations(circuit, merge_func)), expected_circuit)