Exemple #1
0
def test_set_bit():
    sdm = SparseDM(10)
    sdm.set_bit(0, 1)

    assert sdm.classical[0] == 1

    sdm.hadamard(0)
    sdm.hadamard(0)
Exemple #2
0
    def test_multiple_measurement_hadamard_on_classical(self):
        sdm = SparseDM(2)

        sdm.hadamard(0)

        meas = sdm.peak_multiple_measurements([0, 1])

        assert len(meas) == 2
        assert meas == [({0: 0, 1: 0}, 0.5), ({0: 1, 1: 0}, 0.5)]
Exemple #3
0
def test_renormalize():
    sdm = SparseDM(2)

    sdm.hadamard(0)

    sdm.project_measurement(0, 1)

    assert np.allclose(sdm.trace(), 0.5)

    sdm.renormalize()

    assert np.allclose(sdm.trace(), 1)
Exemple #4
0
    def test_multiple_measurement_classical_prob(self):
        sdm = SparseDM(3)

        sdm.hadamard(0)
        sdm.hadamard(1)

        sdm.classical_probability = 0.7

        r = sdm.peak_multiple_measurements([0, 1, 2])

        total_prob = sum(prob for outcome, prob in r)

        assert total_prob == sdm.trace()
Exemple #5
0
    def test_multiple_does_not_change(self):
        sdm = SparseDM(3)

        sdm.hadamard(0)
        sdm.hadamard(1)

        sdm.ensure_dense(0)
        sdm.ensure_dense(1)
        sdm.ensure_dense(2)

        before = sdm.full_dm.to_array()

        assert before.shape == (8, 8)
        sdm.peak_multiple_measurements([0, 1, 2])

        assert np.allclose(before, sdm.full_dm.to_array())
Exemple #6
0
def test_meas_on_hadamard():
    sdm = SparseDM(1)
    sdm.hadamard(0)

    p0, p1 = sdm.peak_measurement(0)

    assert p0 == 0.5
    assert p1 == 0.5

    sdm.project_measurement(0, 1)

    print(sdm.full_dm.to_array())

    assert len(sdm.classical) == 1
    assert sdm.full_dm.no_qubits == 0
    assert sdm.classical[0] == 1
    assert np.allclose(sdm.trace(), 0.5)
Exemple #7
0
    def test_multiple_measurement_hadamard_order2_regression(self):
        sdm = SparseDM(3)

        sdm.hadamard(0)
        sdm.hadamard(1)

        sdm.ensure_dense(0)
        sdm.ensure_dense(1)
        sdm.ensure_dense(2)

        meas = sdm.peak_multiple_measurements([0, 1, 2])

        assert len(meas) == 8
        for state, p in meas:
            print(meas)
            if state[2] == 0:
                assert np.allclose(p, 0.25)
            else:
                assert np.allclose(p, 0)
Exemple #8
0
    def test_multiple_measurement_hadamard_order1(self):
        sdm = SparseDM(3)

        sdm.hadamard(0)
        sdm.hadamard(2)

        sdm.ensure_dense(0)
        sdm.ensure_dense(1)
        sdm.ensure_dense(2)

        meas = sdm.peak_multiple_measurements([0, 1, 2])

        assert len(meas) == 8
        for state, p in meas:
            for x in state.values():
                assert x in [0, 1]
            if state[1] == 0:
                assert np.allclose(p, 0.25)
            else:
                assert np.allclose(p, 0)
Exemple #9
0
    def test_majority_vote_classical_prob(self):

        bits = [1, 2, 3]
        sdm = SparseDM(bits)
        sdm.hadamard(1)
        sdm.hadamard(2)
        sdm.hadamard(3)

        p0 = 0.42
        sdm.classical_probability = p0

        result = {b: 0 for b in bits}
        p = sdm.majority_vote(result)

        assert np.allclose(p, 0.5 * p0)

        sdm.hadamard(3)

        p = sdm.majority_vote(result)

        assert np.allclose(p, 0.75 * p0)

        p = sdm.majority_vote({1: 0, 2: 1, 3: 0})

        assert np.allclose(p, 0.75 * p0)
Exemple #10
0
    def test_majority_after_hadamard(self):

        bits = [1, 2, 3]
        sdm = SparseDM(bits)
        sdm.hadamard(1)
        sdm.hadamard(2)
        sdm.hadamard(3)

        p = sdm.majority_vote(bits)

        assert np.allclose(p, 0.5)

        sdm.hadamard(3)

        p = sdm.majority_vote(bits)

        assert np.allclose(p, 0.25)
Exemple #11
0
    def test_majority_vote_after_hadamard_inverted(self):
        bits = [1, 2, 3]
        sdm = SparseDM(bits)
        sdm.hadamard(1)
        sdm.hadamard(2)
        sdm.hadamard(3)

        result = {b: 0 for b in bits}
        p = sdm.majority_vote(result)

        assert np.allclose(p, 0.5)

        sdm.hadamard(3)

        p = sdm.majority_vote(result)

        assert np.allclose(p, 0.75)

        p = sdm.majority_vote({1: 0, 2: 1, 3: 0})

        assert np.allclose(p, 0.75)
Exemple #12
0
def test_ensure_classical_fail_after_hadamard():
    sdm = SparseDM(10)
    sdm.hadamard(0)
    with pytest.raises(ValueError):
        sdm.ensure_classical(0)