def test_decompose_operations_raises_on_stuck(): c_orig = cirq.Circuit(cirq.X(cirq.NamedQubit("q")).with_tags("ignore")) gateset = cirq.Gateset(cirq.Y) with pytest.raises(ValueError, match="Unable to convert"): _ = _decompose_operations_to_target_gateset(c_orig, gateset=gateset, ignore_failures=False) # Gates marked with a no-compile tag are completely ignored. c_new = _decompose_operations_to_target_gateset( c_orig, context=cirq.TransformerContext(tags_to_ignore=("ignore",)), gateset=gateset, ignore_failures=False, ) cirq.testing.assert_same_circuits(c_orig, c_new)
def test_decompose_operations_to_target_gateset_default(): q = cirq.LineQubit.range(2) c_orig = cirq.Circuit( cirq.T(q[0]), cirq.SWAP(*q), cirq.T(q[0]), cirq.SWAP(*q).with_tags("ignore"), cirq.measure(q[0], key="m"), cirq.X(q[1]).with_classical_controls("m"), cirq.Moment(cirq.T.on_each(*q)), cirq.SWAP(*q), cirq.T.on_each(*q), ) cirq.testing.assert_has_diagram( c_orig, ''' 0: ───T───×───T───×['ignore']───M───────T───×───T─── │ │ ║ │ 1: ───────×───────×─────────────╫───X───T───×───T─── ║ ║ m: ═════════════════════════════@═══^═══════════════''', ) context = cirq.TransformerContext(tags_to_ignore=("ignore",)) c_new = _decompose_operations_to_target_gateset(c_orig, context=context) cirq.testing.assert_has_diagram( c_new, ''' 0: ───T────────────@───Y^-0.5───@───Y^0.5────@───────────T───×['ignore']───M───────T────────────@───Y^-0.5───@───Y^0.5────@───────────T─── │ │ │ │ ║ │ │ │ 1: ───────Y^-0.5───@───Y^0.5────@───Y^-0.5───@───Y^0.5───────×─────────────╫───X───T───Y^-0.5───@───Y^0.5────@───Y^-0.5───@───Y^0.5───T─── ║ ║ m: ════════════════════════════════════════════════════════════════════════@═══^══════════════════════════════════════════════════════════ ''', )
def test_decompose_operations_to_target_gateset(): q = cirq.LineQubit.range(2) c_orig = cirq.Circuit( cirq.T(q[0]), cirq.SWAP(*q), cirq.T(q[0]), cirq.SWAP(*q).with_tags("ignore"), cirq.measure(q[0], key="m"), cirq.X(q[1]).with_classical_controls("m"), cirq.Moment(cirq.T.on_each(*q)), cirq.SWAP(*q), cirq.T.on_each(*q), ) gateset = cirq.Gateset(cirq.H, cirq.CNOT) decomposer = ( lambda op, _: cirq.H(op.qubits[0]) if cirq.has_unitary(op) and cirq.num_qubits(op) == 1 else NotImplemented ) context = cirq.TransformerContext(tags_to_ignore=("ignore",)) c_new = _decompose_operations_to_target_gateset( c_orig, gateset=gateset, decomposer=decomposer, context=context ) cirq.testing.assert_has_diagram( c_new, ''' 0: ───H───@───X───@───H───×['ignore']───M───────H───@───X───@───H─── │ │ │ │ ║ │ │ │ 1: ───────X───@───X───────×─────────────╫───X───H───X───@───X───H─── ║ ║ m: ═════════════════════════════════════@═══^═══════════════════════''', ) with pytest.raises(ValueError, match="Unable to convert"): _ = _decompose_operations_to_target_gateset( c_orig, gateset=gateset, decomposer=decomposer, context=context, ignore_failures=False )