|
15 | 15 | import pytest
|
16 | 16 |
|
17 | 17 | import numpy as np
|
| 18 | +import sympy |
18 | 19 |
|
19 | 20 | import cirq
|
20 | 21 |
|
@@ -49,3 +50,63 @@ def test_assert_decompose_is_consistent_with_unitary():
|
49 | 50 | cirq.testing.assert_decompose_is_consistent_with_unitary(
|
50 | 51 | BadGateDecompose().on(cirq.NamedQubit('q'))
|
51 | 52 | )
|
| 53 | + |
| 54 | + |
| 55 | +class GateDecomposesToDefaultGateset(cirq.Gate): |
| 56 | + def _num_qubits_(self): |
| 57 | + return 2 |
| 58 | + |
| 59 | + def _decompose_(self, qubits): |
| 60 | + return [GoodGateDecompose().on(qubits[0]), BadGateDecompose().on(qubits[1])] |
| 61 | + |
| 62 | + |
| 63 | +class GateDecomposeDoesNotEndInDefaultGateset(cirq.Gate): |
| 64 | + def _num_qubits_(self): |
| 65 | + return 4 |
| 66 | + |
| 67 | + def _decompose_(self, qubits): |
| 68 | + yield GateDecomposeNotImplemented().on_each(*qubits) |
| 69 | + |
| 70 | + |
| 71 | +class GateDecomposeNotImplemented(cirq.SingleQubitGate): |
| 72 | + def _decompose_(self, qubits): |
| 73 | + return NotImplemented |
| 74 | + |
| 75 | + |
| 76 | +class ParameterizedGate(cirq.SingleQubitGate): |
| 77 | + def _num_qubits_(self): |
| 78 | + return 2 |
| 79 | + |
| 80 | + def _decompose_(self, qubits): |
| 81 | + yield cirq.X(qubits[0]) ** sympy.Symbol("x") |
| 82 | + yield cirq.Y(qubits[1]) ** sympy.Symbol("y") |
| 83 | + |
| 84 | + |
| 85 | +def test_assert_decompose_ends_at_default_gateset(): |
| 86 | + |
| 87 | + cirq.testing.assert_decompose_ends_at_default_gateset(GateDecomposesToDefaultGateset()) |
| 88 | + cirq.testing.assert_decompose_ends_at_default_gateset( |
| 89 | + GateDecomposesToDefaultGateset().on(*cirq.LineQubit.range(2)) |
| 90 | + ) |
| 91 | + |
| 92 | + cirq.testing.assert_decompose_ends_at_default_gateset(ParameterizedGate()) |
| 93 | + cirq.testing.assert_decompose_ends_at_default_gateset( |
| 94 | + ParameterizedGate().on(*cirq.LineQubit.range(2)) |
| 95 | + ) |
| 96 | + |
| 97 | + with pytest.raises(AssertionError): |
| 98 | + cirq.testing.assert_decompose_ends_at_default_gateset(GateDecomposeNotImplemented()) |
| 99 | + |
| 100 | + with pytest.raises(AssertionError): |
| 101 | + cirq.testing.assert_decompose_ends_at_default_gateset( |
| 102 | + GateDecomposeNotImplemented().on(cirq.NamedQubit('q')) |
| 103 | + ) |
| 104 | + with pytest.raises(AssertionError): |
| 105 | + cirq.testing.assert_decompose_ends_at_default_gateset( |
| 106 | + GateDecomposeDoesNotEndInDefaultGateset() |
| 107 | + ) |
| 108 | + |
| 109 | + with pytest.raises(AssertionError): |
| 110 | + cirq.testing.assert_decompose_ends_at_default_gateset( |
| 111 | + GateDecomposeDoesNotEndInDefaultGateset().on(*cirq.LineQubit.range(4)) |
| 112 | + ) |
0 commit comments