Пример #1
0
def test_generalized_amplitude_damping_channel_eq():
    et = cirq.testing.EqualsTester()
    c = cirq.generalized_amplitude_damp(0.0, 0.0)
    et.make_equality_group(lambda: c)
    et.add_equality_group(cirq.generalized_amplitude_damp(0.1, 0.0))
    et.add_equality_group(cirq.generalized_amplitude_damp(0.0, 0.1))
    et.add_equality_group(cirq.generalized_amplitude_damp(0.6, 0.4))
    et.add_equality_group(cirq.generalized_amplitude_damp(0.8, 0.2))
Пример #2
0
def main():

    q0 = cirq.LineQubit(0)

    circuit = cirq.Circuit( cirq.generalized_amplitude_damp(9/25,49/625)(q0) )
    print("circuit:")
    print(circuit)

    initial_state = 1

    # sv_simulator = cirq.Simulator()
    dm_simulator = cirq.DensityMatrixSimulator()
    kc_simulator = cirq.KnowledgeCompilationSimulator( circuit, initial_state=initial_state  )

    # sv_result = sv_simulator.simulate( circuit, initial_state=initial_state )
    # print("sv_result.state_vector():")
    # print(sv_result.state_vector())

    dm_result = dm_simulator.simulate( circuit, initial_state=initial_state )
    print("dm_result:")
    print(dm_result)

    kc_result = kc_simulator.simulate( circuit )
    print("kc_result:")
    print(kc_result)

    np.testing.assert_almost_equal(
        kc_result.final_density_matrix,
        dm_result.final_density_matrix,
        decimal=4)
Пример #3
0
def test_generalized_amplitude_damping_channel_text_diagram():
    a = cirq.generalized_amplitude_damp(0.1, 0.39558391)
    assert cirq.circuit_diagram_info(
        a, args=round_to_6_prec) == cirq.CircuitDiagramInfo(
            wire_symbols=('GAD(0.1,0.395584)', ))
    assert cirq.circuit_diagram_info(
        a, args=round_to_2_prec) == cirq.CircuitDiagramInfo(
            wire_symbols=('GAD(0.1,0.4)', ))
Пример #4
0
def test_generalized_amplitude_damping_channel():
    d = cirq.generalized_amplitude_damp(0.1, 0.3)
    np.testing.assert_almost_equal(
        cirq.channel(d),
        (np.sqrt(0.1) * np.array([[1., 0.], [0., np.sqrt(1. - 0.3)]]),
         np.sqrt(0.1) * np.array([[0., np.sqrt(0.3)], [0., 0.]]),
         np.sqrt(0.9) * np.array([[np.sqrt(1. - 0.3), 0.], [0., 1.]]),
         np.sqrt(0.9) * np.array([[0., 0.], [np.sqrt(0.3), 0.]])))
Пример #5
0
def test_generalized_amplitude_damping_channel():
    d = cirq.generalized_amplitude_damp(0.1, 0.3)
    np.testing.assert_almost_equal(
        cirq.channel(d),
        (
            np.sqrt(0.1) * np.array([[1.0, 0.0], [0.0, np.sqrt(1.0 - 0.3)]]),
            np.sqrt(0.1) * np.array([[0.0, np.sqrt(0.3)], [0.0, 0.0]]),
            np.sqrt(0.9) * np.array([[np.sqrt(1.0 - 0.3), 0.0], [0.0, 1.0]]),
            np.sqrt(0.9) * np.array([[0.0, 0.0], [np.sqrt(0.3), 0.0]]),
        ),
    )
    assert cirq.has_channel(d)
    assert not cirq.has_mixture(d)
Пример #6
0
def test_generalized_amplitude_damping_channel_eq():
    a = cirq.generalized_amplitude_damp(0.0099999, 0.01)
    b = cirq.generalized_amplitude_damp(0.01, 0.0099999)

    assert cirq.approx_eq(a, b, atol=1e-2)

    et = cirq.testing.EqualsTester()
    c = cirq.generalized_amplitude_damp(0.0, 0.0)
    et.make_equality_group(lambda: c)
    et.add_equality_group(cirq.generalized_amplitude_damp(0.1, 0.0))
    et.add_equality_group(cirq.generalized_amplitude_damp(0.0, 0.1))
    et.add_equality_group(cirq.generalized_amplitude_damp(0.6, 0.4))
    et.add_equality_group(cirq.generalized_amplitude_damp(0.8, 0.2))
Пример #7
0
def test_channel_simulation():
    q0, q1 = cirq.LineQubit.range(2)
    # These probabilities are set unreasonably high in order to reduce the number
    # of runs required to observe every possible operator.
    amp_damp = cirq.amplitude_damp(gamma=0.5)
    gen_amp_damp = cirq.generalized_amplitude_damp(p=0.4, gamma=0.6)
    cirq_circuit = cirq.Circuit(
        cirq.X(q0)**0.5,
        cirq.X(q1)**0.5,
        amp_damp.on(q0),
        gen_amp_damp.on(q1),
    )

    possible_circuits = [
        cirq.Circuit(cirq.X(q0)**0.5,
                     cirq.X(q1)**0.5, ad, gad)
        # Extract the operators from the channels to construct trajectories.
        for ad in [NoiseStep(m).on(q0) for m in cirq.channel(amp_damp)]
        for gad in [NoiseStep(m).on(q1) for m in cirq.channel(gen_amp_damp)]
    ]
    possible_states = [
        cirq.Simulator().simulate(pc).state_vector()
        for pc in possible_circuits
    ]
    # Since some "gates" were non-unitary, we must normalize.
    possible_states = [ps / np.linalg.norm(ps) for ps in possible_states]

    # Minimize flaky tests with a fixed seed.
    qsimSim = qsimcirq.QSimSimulator(seed=1)
    result_hist = [0] * len(possible_states)
    run_count = 200
    for _ in range(run_count):
        result = qsimSim.simulate(cirq_circuit, qubit_order=[q0, q1])
        for i, ps in enumerate(possible_states):
            if cirq.allclose_up_to_global_phase(result.state_vector(), ps):
                result_hist[i] += 1
                break

    # Each observed result should match one of the possible_results.
    assert sum(result_hist) == run_count
    # Over 200 runs, it's reasonable to expect all eight outcomes.
    assert all(result_count > 0 for result_count in result_hist)
def main():

    q0, q1 = cirq.LineQubit.range(2)
    for iteration in range(3):
        random_circuit = cirq.testing.random_circuit(qubits=[q0, q1],
                                                     n_moments=2,
                                                     op_density=0.99)
        print("random_circuit:")
        print(random_circuit)

        # noise = cirq.ConstantQubitNoiseModel(cirq.asymmetric_depolarize(0.2,0.3,0.4)) # mixture: size four noise not implemented
        # noise = cirq.ConstantQubitNoiseModel(cirq.depolarize(0.1)) # mixture: size four noise not implemented
        # noise = cirq.ConstantQubitNoiseModel(cirq.phase_flip(0.1)) # mixture: works well
        # noise = cirq.ConstantQubitNoiseModel(cirq.bit_flip(0.1)) # mixture: works well

        noise = cirq.ConstantQubitNoiseModel(
            cirq.generalized_amplitude_damp(
                9 / 25, 49 / 625))  # channel: size four noise not implemented
        # noise = cirq.ConstantQubitNoiseModel(cirq.amplitude_damp(0.1)) # channel:
        # noise = cirq.ConstantQubitNoiseModel(cirq.phase_damp(0.1)) # channel:
        # reset?

        kc_simulator = cirq.KnowledgeCompilationSimulator(random_circuit,
                                                          noise=noise,
                                                          intermediate=True)
        dm_simulator = cirq.DensityMatrixSimulator(noise=noise)

        for initial_state in range(3):

            kc_result = kc_simulator.simulate(random_circuit,
                                              initial_state=initial_state)
            print("kc_result:")
            print(kc_result)

            dm_result = dm_simulator.simulate(random_circuit,
                                              initial_state=initial_state)
            print("dm_result:")
            print(dm_result)

            np.testing.assert_almost_equal(kc_result.final_density_matrix,
                                           dm_result.final_density_matrix,
                                           decimal=5)
Пример #9
0
def _get_noise_proto_pairs():
    q0 = cirq.GridQubit(0, 0)

    pairs = [
        # Depolarization.
        (cirq.Circuit(cirq.depolarize(p=0.3)(q0)),
         _build_op_proto("DP", ['p'], [0.3], ['0_0'])),

        # Asymmetric depolarization.
        (cirq.Circuit(
            cirq.asymmetric_depolarize(p_x=0.1, p_y=0.2, p_z=0.3)(q0)),
         _build_op_proto("ADP", ['p_x', 'p_y', 'p_z'], [0.1, 0.2, 0.3],
                         ['0_0'])),

        # Generalized Amplitude damp.
        (cirq.Circuit(cirq.generalized_amplitude_damp(p=0.1, gamma=0.2)(q0)),
         _build_op_proto("GAD", ['p', 'gamma'], [0.1, 0.2], ['0_0'])),

        # Amplitude damp.
        (cirq.Circuit(cirq.amplitude_damp(gamma=0.1)(q0)),
         _build_op_proto("AD", ['gamma'], [0.1], ['0_0'])),

        # Reset.
        (cirq.Circuit(cirq.reset(q0)), _build_op_proto("RST", [], [],
                                                       ['0_0'])),

        # Phase damp.
        (cirq.Circuit(cirq.phase_damp(gamma=0.1)(q0)),
         _build_op_proto("PD", ['gamma'], [0.1], ['0_0'])),

        # Phase flip.
        (cirq.Circuit(cirq.phase_flip(p=0.1)(q0)),
         _build_op_proto("PF", ['p'], [0.1], ['0_0'])),

        # Bit flip.
        (cirq.Circuit(cirq.bit_flip(p=0.1)(q0)),
         _build_op_proto("BF", ['p'], [0.1], ['0_0']))
    ]
    return pairs
Пример #10
0
def test_generalized_amplitude_damping_channel_bigly_probability(p, gamma):
    with pytest.raises(ValueError, match='was greater than 1'):
        cirq.generalized_amplitude_damp(p, gamma)
Пример #11
0
def test_generalized_amplitude_damping_channel_negative_probability(p, gamma):
    with pytest.raises(ValueError, match='was less than 0'):
        cirq.generalized_amplitude_damp(p, gamma)
Пример #12
0
def test_generalized_amplitude_damping_str():
    assert (str(cirq.generalized_amplitude_damp(
        0.1, 0.3)) == 'generalized_amplitude_damp(p=0.1,gamma=0.3)')
Пример #13
0
def test_generalized_amplitude_damping_channel_text_diagram():
    a = cirq.generalized_amplitude_damp(0.1, 0.3)
    assert (cirq.circuit_diagram_info(a) == cirq.CircuitDiagramInfo(
        wire_symbols=('GAD(0.1,0.3)', )))