コード例 #1
0
def test_pstring_collection_empty():
    pauli_collection = PauliStringCollection()

    assert pauli_collection.elements == []
    assert pauli_collection.elements_by_weight == {}
    assert pauli_collection.min_weight() == 0
    assert pauli_collection.support() == set()
    assert len(pauli_collection) == 0
コード例 #2
0
def test_pstring_collection_len():
    x = PauliString(spec="X", support=(0,))
    y = PauliString(spec="Y", support=(1,))
    z = PauliString(spec="Z", support=(2,))
    assert len(PauliStringCollection(x, y, z)) == 3
    assert len(PauliStringCollection(x, x, x)) == 3
    assert len(PauliStringCollection(x, y)) == 2
    assert len(PauliStringCollection(x)) == 1
    assert len(PauliStringCollection()) == 0
コード例 #3
0
def test_pstring_collection_str():
    x = PauliString(spec="X")
    iz = PauliString(spec="IZ")
    pcol = PauliStringCollection(x, iz)
    assert str(pcol) == "X(0) + Z(1)"

    xz = PauliString(spec="XZ", coeff=-2.4)
    pcol.add(xz)
    assert str(pcol) == "X(0) + Z(1) + (-2.4+0j)*X(0)*Z(1)"
コード例 #4
0
ファイル: test_observable.py プロジェクト: unitaryfund/mitiq
def test_observable_from_pauli_string_collections():
    z = PauliString("Z")
    zz = PauliString("ZZ")
    pcol1 = PauliStringCollection(z, zz)
    x = PauliString("X")
    xx = PauliString("XX")
    pcol2 = PauliStringCollection(x, xx)

    obs = Observable.from_pauli_string_collections(pcol1, pcol2)
    assert obs.ngroups == 2
    assert obs.groups[0] == pcol1
    assert obs.groups[1] == pcol2
    assert obs.nterms == 4
コード例 #5
0
ファイル: test_observable.py プロジェクト: unitaryfund/mitiq
def test_observable_partition_single_qubit_paulis():
    x = PauliString(spec="X")
    y = PauliString(spec="Y")
    z = PauliString(spec="Z")
    obs = Observable(x, y, z)
    assert obs.nterms == 3

    obs.partition(seed=2)
    expected_groups = [
        PauliStringCollection(x),
        PauliStringCollection(y),
        PauliStringCollection(z),
    ]
    assert obs.groups == expected_groups
コード例 #6
0
def test_pstringcollection():
    x = PauliString(spec="X")
    iz = PauliString(spec="IZ")
    xz = PauliString(spec="XZ")
    xzixx = PauliString(spec="XZIXX")
    pauli_collection = PauliStringCollection(x, iz, xz, xzixx)

    assert pauli_collection.elements == [x, iz, xz, xzixx]
    assert pauli_collection.elements_by_weight == {
        1: Counter((x, iz)),
        2: Counter((xz,)),
        4: Counter((xzixx,)),
    }
    assert pauli_collection.min_weight() == 1
    assert pauli_collection.max_weight() == 4
    assert pauli_collection.support() == {0, 1, 3, 4}
    assert len(pauli_collection) == 4
コード例 #7
0
def test_pstring_collection_eq():
    x = PauliString(spec="X")
    z = PauliString(spec="IZ")
    xz = PauliString(spec="XZ")
    xzz = PauliString(spec="IIIXZZ")

    assert PauliStringCollection(x, xzz) == PauliStringCollection(xzz, x)
    assert PauliStringCollection(x, z) != PauliStringCollection(x, xz)
    assert PauliStringCollection(x, z, xz) == PauliStringCollection(z, xz, x)
    assert PauliStringCollection() == PauliStringCollection()
コード例 #8
0
ファイル: test_observable.py プロジェクト: unitaryfund/mitiq
def test_observable_partition_one_set():
    pauli1 = PauliString(spec="ZI")
    pauli2 = PauliString(spec="IZ")
    pauli3 = PauliString(spec="ZZ")
    obs = Observable(pauli1, pauli2, pauli3)
    assert obs.nterms == 3

    assert obs.ngroups == 1
    assert obs.groups[0] == PauliStringCollection(pauli1, pauli2, pauli3)
コード例 #9
0
def test_pstringcollection_expectation_from_measurements_qubit_indices():
    measurements = MeasurementResult([[0, 0], [0, 0], [0, 1], [0, 1]],
                                     qubit_indices=(1, 5))
    pset = PauliStringCollection(
        PauliString(spec="Z", coeff=-2.0, support=(1, )))
    assert np.isclose(pset._expectation_from_measurements(measurements), -2.0)

    pset = PauliStringCollection(
        PauliString(spec="Z", coeff=-2.0, support=(5, )))
    assert np.isclose(pset._expectation_from_measurements(measurements), 0.0)
コード例 #10
0
def test_pstringcollection_expectation_from_measurements():
    measurements = MeasurementResult([[0, 0], [0, 0], [0, 1], [0, 1]])
    pset = PauliStringCollection(PauliString(spec="ZI", coeff=-2.0),
                                 PauliString(spec="IZ", coeff=5.0))
    assert np.isclose(pset._expectation_from_measurements(measurements), -2.0)

    measurements = MeasurementResult([[1, 0], [1, 0], [0, 1], [0, 1]])
    pset = PauliStringCollection(PauliString(spec="ZI", coeff=-2.0),
                                 PauliString(spec="IZ", coeff=5.0))
    assert np.isclose(pset._expectation_from_measurements(measurements), 0.0)
コード例 #11
0
ファイル: observable.py プロジェクト: unitaryfund/mitiq
    def partition(self, seed: Optional[int] = None) -> None:
        rng = np.random.RandomState(seed)

        psets: List[PauliStringCollection] = []
        paulis = copy.deepcopy(self._paulis)
        rng.shuffle(paulis)

        while paulis:
            pauli = paulis.pop()
            added = False
            for (i, pset) in enumerate(psets):
                if pset.can_add(pauli):
                    pset.add(pauli)
                    added = True
                    break

            if not added:
                psets.append(PauliStringCollection(pauli))

        self._groups = psets
        self._ngroups = len(self._groups)
コード例 #12
0
def test_pstring_collection_add():
    pcol = PauliStringCollection()

    a = PauliString(spec="ZZ")
    assert pcol.can_add(a)
    pcol.add(a)
    assert pcol.elements == [a]

    b = PauliString(spec="ZIXZ")
    assert pcol.can_add(b)
    pcol.add(b)
    assert pcol.elements == [a, b]

    assert pcol.can_add(a)
    pcol.add(a)
    assert pcol.elements == [a, a, b]

    c = PauliString(spec="YY")
    assert not pcol.can_add(c)
    with pytest.raises(ValueError, match="Cannot add PauliString"):
        pcol.add(c)