Skip to content

Commit

Permalink
Revert "Change mp_to_pauli to be more general"
Browse files Browse the repository at this point in the history
This reverts commit a1ff606.
  • Loading branch information
austingmhuang committed Apr 24, 2024
1 parent 160b14c commit 4386d44
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
44 changes: 37 additions & 7 deletions pennylane_qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,23 +709,53 @@ def mp_to_pauli(mp, register_size):

# ToDo: I believe this could be extended to cover expectation values of Hamiltonians

observables = {"PauliX": "X", "PauliY": "Y", "PauliZ": "Z", "Identity": "I"}
pauli_strings = []
coeffs = []
op = mp.obs
coeffs = None

pauli_strings = []
if hasattr(mp.obs, "coeffs"):
coeffs = mp.obs.coeffs

if isinstance(mp.obs, qml.Hamiltonian):
mp.obs = mp.obs.ops

if not isinstance(mp.obs, list):
mp.obs = [mp.obs]

for pauli_term, coeff in op.pauli_rep.items():
for obs in mp.obs:
pauli_string = ["I"] * register_size
coeffs.append(coeff)
if len(pauli_term.wires) == 1:
pauli_string[pauli_term.wires[0]] = pauli_term[pauli_term.wires[0]]
for i in range(len(obs.wires)):
if isinstance(obs.name, str):
pauli_string[obs.wires[i]] = observables[obs.name]
else:
pauli_string[obs.wires[i]] = observables[obs.name[i]]

pauli_string.reverse()
pauli_string = ("").join(pauli_string)
pauli_strings.append(pauli_string)

return SparsePauliOp(data = pauli_strings, coeffs = coeffs)

# if len(mp.wires) == 1:
# pauli_string[mp.wires[0]] = observables[mp.obs.name]
# elif isinstance(mp.obs, qml.Hamiltonian):
# for observable in mp.obs.ops:
# print(observable)
# for i in range(len(observable.wires)):
# if isinstance(observable.name, str):
# pauli_string[observable.wires[i]] = observables[observable.name]
# else:
# pauli_string[observable.wires[i]] = observables[observable.name[i]]


# for observable in mp.obs:
# pauli_string[observable.wires[0]] = observables[observable.name]

# Qiskit orders wires in the opposite direction compared to PL
#pauli_string.reverse()

#pauli_string = ("").join(pauli_string)



def load_pauli_op(
Expand Down
44 changes: 41 additions & 3 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from qiskit.circuit.parametervector import ParameterVectorElement
from qiskit.exceptions import QiskitError
from qiskit.quantum_info import Operator, SparsePauliOp
from semantic_version import Version

import pennylane as qml
from pennylane import numpy as np
Expand Down Expand Up @@ -1775,12 +1774,51 @@ def test_mp_to_pauli(self, measurement_type, observable, obs_string, wire, regis
# remaining wires are all Identity
assert np.all([op == "I" for op in pauli_op_list])

def test_mp_to_pauli_for_hamiltonian(self):
@pytest.mark.parametrize("measurement_type", [qml.expval, qml.var])
@pytest.mark.parametrize("register_size", [3, 5])
@pytest.mark.parametrize("wire", [0, 1, 2])
@pytest.mark.parametrize("coeff", [2, 3, -2])
@pytest.mark.parametrize(
"observable, obs_string",
[(qml.PauliX, "X"), (qml.PauliY, "Y"), (qml.PauliZ, "Z"), (qml.Identity, "I")],
)
def test_mp_to_pauli_for_scalar(self, measurement_type, register_size, wire, coeff, observable, obs_string):
"""Tests that a SparsePauliOp is created from a Hamiltonian, and that
it has the expected format"""
assert True
op = coeff * observable(wire)
obs = measurement_type(op)
data = ""
for i in range(register_size - 1, -1, -1):
if i == wire:
data += obs_string
else:
data += "I"

pauli_op = mp_to_pauli(obs, register_size)
assert isinstance(pauli_op, SparsePauliOp)

pauli_op_list = list(pauli_op.paulis.to_labels()[0])

# all qubits in register are accounted for
assert len(pauli_op_list) == register_size
assert pauli_op == SparsePauliOp(data=data, coeffs=[coeff])


@pytest.mark.parametrize("measurement_type", [qml.expval])
@pytest.mark.parametrize("register_size", [3])
def test_mp_to_pauli_for_hamiltonian(self, measurement_type, register_size):
"""Tests that a SparsePauliOp is created from a Hamiltonian, and that
it has the expected format"""
hamiltonian = qml.Hamiltonian([1, 2], [qml.X(0) @ qml.Z(1), qml.Z(2) @ qml.Y(1)]) ## 1 * X(0) @ Z (1) + 2 * Z(2) @ Y (1)
obs = measurement_type(hamiltonian)

pauli_op = mp_to_pauli(obs, register_size)
assert isinstance(pauli_op, SparsePauliOp)

pauli_op_list = list(pauli_op.paulis.to_labels()[0])

# all qubits in register are accounted for
assert len(pauli_op_list) == register_size


class TestControlOpIntegration:
Expand Down

0 comments on commit 4386d44

Please sign in to comment.