def test_PrepareAndMeasureOnWFSim():
    p = Program()
    params = p.declare("params", memory_type="REAL", memory_size=2)
    p.inst(RX(params[0], 0))
    p.inst(RX(params[1], 1))

    def make_memory_map(params):
        return {"params": params}


#    ham = PauliSum.from_compact_str("1.0*Z0 + 1.0*Z1")

    term1 = PauliTerm("Z", 0)
    term2 = PauliTerm("Z", 1)
    ham = PauliSum([term1, term2])
    sim = WavefunctionSimulator()
    with local_qvm():
        cost_fn = PrepareAndMeasureOnWFSim(p,
                                           make_memory_map,
                                           ham,
                                           sim,
                                           scalar_cost_function=False,
                                           enable_logging=True)
        out = cost_fn([np.pi, np.pi / 2], nshots=100)
        print(cost_fn.log)
        assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.1))
        assert np.allclose(out, (-1, 0.1))
def test_PrepareAndMeasureOnWFSim_QubitPlaceholders():
    q1, q2 = QubitPlaceholder(), QubitPlaceholder()
    p = Program()
    params = p.declare("params", memory_type="REAL", memory_size=2)
    p.inst(RX(params[0], q1))
    p.inst(RX(params[1], q2))

    def make_memory_map(params):
        return {"params": params}

    ham = PauliSum([PauliTerm("Z", q1), PauliTerm("Z", q2)])
    qubit_mapping = get_default_qubit_mapping(p)
    sim = WavefunctionSimulator()
    with local_qvm():
        cost_fn = PrepareAndMeasureOnWFSim(
            p,
            make_memory_map,
            ham,
            sim,
            enable_logging=True,
            qubit_mapping=qubit_mapping,
            scalar_cost_function=False,
        )
        out = cost_fn([np.pi, np.pi / 2], nshots=100)
        assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.1))
        assert np.allclose(out, (-1, 0.1))
Beispiel #3
0
def test_vqe_on_WFSim():
    sim = WavefunctionSimulator()
    cost_fun = PrepareAndMeasureOnWFSim(prepare_ansatz=prepare_ansatz,
                                        make_memory_map=lambda p: {"params": p},
                                        hamiltonian=hamiltonian,
                                        sim=sim,
                                        scalar_cost_function=True)

    out = minimize(cost_fun, p0, tol=1e-3, method="COBYLA")
    wf = sim.wavefunction(prepare_ansatz, {"params": out['x']})
    assert np.allclose(wf.probabilities(), [0, 0, 0, 1], rtol=1.5, atol=0.01)
    assert np.allclose(out['fun'], -1.3)
    assert out['success']
def test_vqe_on_WFSim():
    sim = WavefunctionSimulator()
    cost_fun = PrepareAndMeasureOnWFSim(
        prepare_ansatz=prepare_ansatz,
        make_memory_map=lambda p: {"params": p},
        hamiltonian=hamiltonian,
        sim=sim,
        return_standard_deviation=True,
        noisy=False)

    with local_qvm():
        out = scipy_optimizer(cost_fun, p0, epsilon=1e-3)
        print(out)
        wf = sim.wavefunction(prepare_ansatz, {"params": out['x']})
    assert np.allclose(np.abs(wf.amplitudes**2), [0, 0, 0, 1],
                       rtol=1.5,
                       atol=0.01)
    assert np.allclose(out['fun'], -1.3)
    assert out['success']
def test_vqe_on_WFSim_QubitPlaceholders():
    qubit_mapping = get_default_qubit_mapping(prepare_ansatz)
    sim = WavefunctionSimulator()
    cost_fun = PrepareAndMeasureOnWFSim(
        prepare_ansatz=prepare_ansatz,
        make_memory_map=lambda p: {"params": p},
        hamiltonian=hamiltonian,
        sim=sim,
        scalar_cost_function=True,
        qubit_mapping=qubit_mapping)

    out = scipy.optimize.minimize(cost_fun, p0, tol=1e-3, method="Cobyla")
    print(out)
    prog = address_qubits(prepare_ansatz, qubit_mapping=qubit_mapping)
    wf = sim.wavefunction(prog, {"params": out['x']})
    print(wf.probabilities())
    assert np.allclose(np.abs(wf.amplitudes**2), [0, 0, 0, 1],
                       rtol=1.5,
                       atol=0.01)
    assert np.allclose(out['fun'], -4)
    assert out['success']