コード例 #1
0
def test_dephase():
    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit(
        cirq.CircuitOperation(
            cirq.FrozenCircuit(
                cirq.CX(q1, q0),
                cirq.measure(q0, key='a'),
                cirq.CX(q0, q1),
                cirq.measure(q1, key='b'),
            )))
    assert_equivalent_to_dephased(circuit)
    dephased = cirq.dephase_measurements(circuit)
    cirq.testing.assert_same_circuits(
        dephased,
        cirq.Circuit(
            cirq.CircuitOperation(
                cirq.FrozenCircuit(
                    cirq.CX(q1, q0),
                    cirq.KrausChannel.from_channel(cirq.phase_damp(1),
                                                   key='a')(q0),
                    cirq.CX(q0, q1),
                    cirq.KrausChannel.from_channel(cirq.phase_damp(1),
                                                   key='b')(q1),
                ))),
    )
コード例 #2
0
def test_dephase_nocompile_context():
    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit(
        cirq.CircuitOperation(
            cirq.FrozenCircuit(
                cirq.CX(q1, q0),
                cirq.measure(q0, key='a').with_tags('nocompile'),
                cirq.CX(q0, q1),
                cirq.measure(q1, key='b'),
            )))
    dephased = cirq.dephase_measurements(
        circuit,
        context=cirq.TransformerContext(tags_to_ignore=('nocompile', )))
    cirq.testing.assert_same_circuits(
        dephased,
        cirq.Circuit(
            cirq.CircuitOperation(
                cirq.FrozenCircuit(
                    cirq.CX(q1, q0),
                    cirq.measure(q0, key='a').with_tags('nocompile'),
                    cirq.CX(q0, q1),
                    cirq.KrausChannel.from_channel(cirq.phase_damp(1),
                                                   key='b')(q1),
                ))),
    )
コード例 #3
0
def test_dephase_classical_conditions():
    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit(
        cirq.measure(q0, key='a'),
        cirq.X(q1).with_classical_controls('a'),
        cirq.measure(q1, key='b'),
    )
    with pytest.raises(ValueError, match='defer_measurements first to remove classical controls'):
        _ = cirq.dephase_measurements(circuit)
コード例 #4
0
def assert_equivalent_to_dephased(circuit: cirq.Circuit):
    qubits = list(circuit.all_qubits())
    with cirq.testing.assert_deprecated('ignore_measurement_results',
                                        deadline='v0.15',
                                        count=14):
        sim = cirq.DensityMatrixSimulator(ignore_measurement_results=True)
        num_qubits = len(qubits)
        backwards = list(circuit.all_operations())[::-1]
        for j in range(num_qubits):
            backwards.append(cirq.H(qubits[j])**np.random.rand())
        modified = cirq.Circuit(backwards[::-1])
        for j in range(num_qubits):
            modified.append(cirq.H(qubits[j])**np.random.rand())
        dephased = cirq.dephase_measurements(modified)
        result = sim.simulate(modified)
        result1 = sim.simulate(dephased)
        np.testing.assert_almost_equal(result.final_density_matrix,
                                       result1.final_density_matrix)