Exemple #1
0
def test_annealing_to_standard():
    params = AnnealingParams.linear_ramp_from_hamiltonian(hamiltonian,
                                                          2,
                                                          time=2)
    params2 = annealing_to_standard(params)
    assert np.allclose(params.x_rotation_angles, params2.x_rotation_angles)
    assert np.allclose(params.z_rotation_angles, params2.z_rotation_angles)
    assert np.allclose(params.zz_rotation_angles, params2.zz_rotation_angles)
def test_QAOACostFunctionOnQVM():
    qvm = get_qc("2q-qvm")
    params = AnnealingParams.linear_ramp_from_hamiltonian(hamiltonian, n_steps=4)

    cost_function = QAOACostFunctionOnQVM(hamiltonian,
                                          params=params,
                                          qvm=qvm,
                                          scalar_cost_function=False)
    out = cost_function(params.raw(), nshots=1)
    print(out)
def test_QAOAParameterIterator():
    params = AnnealingParams.linear_ramp_from_hamiltonian(hamiltonian, 2)
    iterator = QAOAParameterIterator(params, "schedule[0]",
                                     np.arange(0, 1, 0.5))
    log = []
    for p in iterator:
        log.append((p.schedule).copy())
    print(log[0])
    print(log[1])
    assert np.allclose(log[0], [0, 0.75])
    assert np.allclose(log[1], [0.5, 0.75])
def test_QAOACostFunctionOnWFSim():
    sim = WavefunctionSimulator()
    params = AnnealingParams.linear_ramp_from_hamiltonian(hamiltonian, n_steps=4)

    cost_function = QAOACostFunctionOnWFSim(hamiltonian,
                                            params=params,
                                            sim=sim,
                                            scalar_cost_function=False,
                                            enable_logging=True)
    out = cost_function(params.raw(), nshots=100)
    print(out)
def test_AnnealingParamsfromAbstractParameters():
    abstract_params = AbstractParams((hamiltonian, 2))
    schedule = [0.4, 1.0]
    parameters = (schedule)
    adiabatic_params = AnnealingParams.from_AbstractParameters(abstract_params,
                                                               parameters,
                                                               time=5.0)
    print("The rotation angles from AnnealingParams.fromAbstractParameters")
    print("x_rotation_angles:\n", adiabatic_params.x_rotation_angles)
    print("z_rotation_angles:\n", adiabatic_params.z_rotation_angles)
    print("zz_rotation_angles:\n", adiabatic_params.zz_rotation_angles)
    assert type(adiabatic_params) == AnnealingParams
def test_AnnealingParams():
    params = AnnealingParams.linear_ramp_from_hamiltonian(hamiltonian,
                                                          2,
                                                          time=2)
    assert set(params.reg) == {0, 1, q1}
    assert np.allclose(params.x_rotation_angles, [[0.75] * 3, [0.25] * 3])
    assert np.allclose(params.z_rotation_angles, [[0.125], [0.375]])
    assert np.allclose(params.zz_rotation_angles, [[0.25, -0.5], [0.75, -1.5]])
    assert [params.qubits_singles
            ] == [term.get_qubits() for term in hamiltonian if len(term) == 1]
    # Test updating and raw output
    raw = np.random.rand(len(params))
    params.update_from_raw(raw)
    assert np.allclose(raw, params.raw())
Exemple #7
0
def test_QAOACostFunctionOnWFSim():
    sim = WavefunctionSimulator()
    params = AnnealingParams.linear_ramp_from_hamiltonian(hamiltonian,
                                                          n_steps=4)

    with local_qvm():
        cost_function = QAOACostFunctionOnWFSim(hamiltonian,
                                                params=params,
                                                sim=sim,
                                                scalar_cost_function=False,
                                                noisy=True,
                                                enable_logging=True)
        out = cost_function(params.raw(), nshots=100)
        print("Log:", cost_function.log)
        print("output of QAOACostFunctionOnWFSim: ", out)
def test_parameter_empty():
    p = ExtendedParams.empty((hamiltonian, 4))
    assert isinstance(p, ExtendedParams)
    assert p.betas.shape == (4, 3)
    assert p.gammas_singles.shape == (4, 1)
    assert p.gammas_pairs.shape == (4, 2)

    p = StandardParams.empty((hamiltonian, 4))
    assert isinstance(p, StandardParams)
    assert p.betas.shape == (4, )
    assert p.gammas.shape == (4, )

    p = StandardWithBiasParams.empty((hamiltonian, 4))
    assert isinstance(p, StandardWithBiasParams)
    assert p.betas.shape == (4, )
    assert p.gammas_singles.shape == (4, )
    assert p.gammas_pairs.shape == (4, )

    p = AnnealingParams.empty((hamiltonian, 4, 2.0))
    assert isinstance(p, AnnealingParams)
    assert p.schedule.shape == (4, )

    p = FourierParams.empty((hamiltonian, 4, 2))
    assert isinstance(p, FourierParams)
    assert p.u.shape == (2, )
    assert p.v.shape == (2, )

    p = FourierWithBiasParams.empty((hamiltonian, 4, 2))
    assert isinstance(p, FourierWithBiasParams)
    assert p.u_singles.shape == (2, )
    assert p.u_pairs.shape == (2, )
    assert p.v.shape == (2, )

    p = FourierExtendedParams.empty((hamiltonian, 4, 2))
    assert isinstance(p, FourierExtendedParams)
    assert p.u_singles.shape == (2, 1)
    assert p.u_pairs.shape == (2, 2)
    assert p.v.shape == (2, 3)