Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
austingmhuang committed Apr 24, 2024
1 parent 4386d44 commit f71c1d4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 87 deletions.
46 changes: 5 additions & 41 deletions pennylane_qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,52 +710,16 @@ 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 = None

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 obs in mp.obs:
pauli_string = ["I"] * register_size
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]
pauli_string = ["I"] * register_size
pauli_string[mp.wires[0]] = observables[mp.obs.name]

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

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

return SparsePauliOp(pauli_string)


def load_pauli_op(
Expand Down
47 changes: 1 addition & 46 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,7 @@ def test_with_predefined_creg(self):


class TestConverterUtilsPennyLaneToQiskit:

@pytest.mark.parametrize("measurement_type", [qml.expval, qml.var])
@pytest.mark.parametrize(
"observable, obs_string",
Expand Down Expand Up @@ -1774,52 +1775,6 @@ 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])

@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"""
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:
"""Test the controlled flows integration with PennyLane"""
Expand Down

0 comments on commit f71c1d4

Please sign in to comment.