示例#1
0
def test_convert_to_cz_preserving_moment_structure():
    q = cirq.LineQubit.range(5)
    op = lambda q0, q1: cirq.H(q1).controlled_by(q0)
    c_orig = cirq.Circuit(
        cirq.Moment(cirq.X(q[2])),
        cirq.Moment(op(q[0], q[1]), op(q[2], q[3])),
        cirq.Moment(op(q[2], q[1]), op(q[4], q[3])),
        cirq.Moment(op(q[1], q[2]), op(q[3], q[4])),
        cirq.Moment(op(q[3], q[2]), op(q[1], q[0])),
        cirq.measure(*q[:2], key="m"),
        cirq.X(q[2]).with_classical_controls("m"),
        cirq.CZ(*q[3:]).with_classical_controls("m"),
    )
    c_new = cirq.optimize_for_target_gateset(c_orig,
                                             gateset=cirq.CZTargetGateset())

    assert c_orig[-2:] == c_new[-2:]
    c_orig, c_new = c_orig[:-2], c_new[:-2]

    cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(
        c_orig, c_new, atol=1e-6)
    assert all(
        (all_gates_of_type(m, cirq.Gateset(cirq.AnyUnitaryGateFamily(1)))
         or all_gates_of_type(m, cirq.Gateset(cirq.CZ))) for m in c_new)

    c_new = cirq.optimize_for_target_gateset(
        c_orig,
        gateset=cirq.CZTargetGateset(allow_partial_czs=True),
        ignore_failures=False)
    cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(
        c_orig, c_new, atol=1e-6)
    assert all(
        (all_gates_of_type(m, cirq.Gateset(cirq.AnyUnitaryGateFamily(1)))
         or all_gates_of_type(m, cirq.Gateset(cirq.CZPowGate))) for m in c_new)
示例#2
0
def test_any_unitary_gate_family():
    with pytest.raises(ValueError, match='must be a positive integer'):
        _ = cirq.AnyUnitaryGateFamily(0)

    for num_qubits in range(1, 6, 2):
        q = cirq.LineQubit.range(num_qubits)
        gate = UnitaryGate(num_qubits)
        for init_num_qubits in [None, num_qubits]:
            gate_family = cirq.AnyUnitaryGateFamily(init_num_qubits)
            cirq.testing.assert_equivalent_repr(gate_family)
            assert gate in gate_family
            assert gate(*q) in gate_family
            if init_num_qubits:
                assert f'{init_num_qubits}' in gate_family.name
                assert f'{init_num_qubits}' in gate_family.description
                assert UnitaryGate(num_qubits + 1) not in gate_family
            else:
                assert f'Any-Qubit' in gate_family.name
                assert f'any unitary' in gate_family.description

    assert cirq.SingleQubitGate() not in cirq.AnyUnitaryGateFamily()
示例#3
0
 def __init__(self):
     super().__init__(cirq.AnyUnitaryGateFamily(1), cirq.CNOT)
示例#4
0
 def __init__(self):
     super().__init__(cirq.AnyUnitaryGateFamily(2))
示例#5
0
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import cirq
import pytest
import numpy as np
import cirq_google as cg
import sympy

EXPECTED_TARGET_GATESET = cirq.Gateset(cirq.AnyUnitaryGateFamily(1), cg.SYC)


def assert_implements(circuit: cirq.Circuit, target_op: cirq.Operation):
    assert all(op in EXPECTED_TARGET_GATESET for op in circuit.all_operations())
    assert sum(1 for _ in circuit.findall_operations(lambda e: len(e.qubits) > 2)) <= 6
    circuit.append(cirq.I.on_each(*target_op.qubits))
    cirq.testing.assert_allclose_up_to_global_phase(
        cirq.unitary(circuit), cirq.unitary(target_op), atol=1e-7
    )


theta = sympy.Symbol('theta')
all_exps = np.linspace(0, 1, 10)
q = cirq.LineQubit.range(2)