From f71c1d495274db9beaff69a13922f42d04269368 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Wed, 24 Apr 2024 13:14:41 -0400 Subject: [PATCH] revert --- pennylane_qiskit/converter.py | 46 ++++------------------------------ tests/test_converter.py | 47 +---------------------------------- 2 files changed, 6 insertions(+), 87 deletions(-) diff --git a/pennylane_qiskit/converter.py b/pennylane_qiskit/converter.py index acda7c025..76e4d5744 100644 --- a/pennylane_qiskit/converter.py +++ b/pennylane_qiskit/converter.py @@ -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( diff --git a/tests/test_converter.py b/tests/test_converter.py index b7b8160df..8da6c762a 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -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", @@ -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"""