Exemple #1
0
def test_probabilities():
    p = 1
    n_qubits = 2
    # known set of angles for barbell
    angles = [1.96348709, 4.71241069]

    wf = np.array([
        -1.17642098e-05 - 1j * 7.67538040e-06,
        -7.67563580e-06 - 1j * 7.07106781e-01,
        -7.67563580e-06 - 1j * 7.07106781e-01,
        -1.17642098e-05 - 1j * 7.67538040e-06
    ])
    with patch("grove.pyqaoa.qaoa.WavefunctionSimulator") as mock_wfs:
        fake_wfs = Mock(WavefunctionSimulator)
        fake_wfs.wavefunction.return_value = Wavefunction(wf)
        mock_wfs.return_value = fake_wfs

        fake_qc = Mock(QuantumComputer)
        inst = QAOA(qc=fake_qc,
                    qubits=list(range(n_qubits)),
                    steps=p,
                    rand_seed=42)

        probs = inst.probabilities(angles)
        assert isinstance(probs, np.ndarray)

    prob_true = np.zeros((inst.nstates, 1))
    prob_true[1] = 0.5
    prob_true[2] = 0.5
    assert np.isclose(probs, prob_true).all()
Exemple #2
0
def test_probabilities():
    p = 1
    n_qubits = 2
    # known set of angles for barbell
    angles = [1.96348709,  4.71241069]
    wf = np.array([-1.17642098e-05 - 1j*7.67538040e-06,
                   -7.67563580e-06 - 1j*7.07106781e-01,
                   -7.67563580e-06 - 1j*7.07106781e-01,
                   -1.17642098e-05 - 1j*7.67538040e-06])
    fakeQVM = Mock(spec=qvm_module.SyncConnection())
    fakeQVM.wavefunction = Mock(return_value=(Wavefunction(wf), 0))
    inst = QAOA(fakeQVM, n_qubits, steps=p,
                rand_seed=42)

    true_probs = np.zeros_like(wf)
    for xx in range(wf.shape[0]):
        true_probs[xx] = np.conj(wf[xx]) * wf[xx]
    probs = inst.probabilities(angles)
    assert isinstance(probs, np.ndarray)
    prob_true = np.zeros((2**inst.n_qubits, 1))
    prob_true[1] = 0.5
    prob_true[2] = 0.5
    assert np.isclose(probs, prob_true).all()