Skip to content

Commit

Permalink
Remove Tensor in devices (#204)
Browse files Browse the repository at this point in the history
* remove Tensor in devices

* fix wrong variable name and format

* fix bug

* temp addition: where are we testing this?

* update changelog

* remove temp change

* trigger ci
  • Loading branch information
lillian542 authored Nov 18, 2024
1 parent e94c315 commit 21c6cf8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 52 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* The ``qml.QubitStateVector`` template has been removed. Instead, use :class:`~pennylane.StatePrep`.
[(#203)](https://github.com/PennyLaneAI/pennylane-cirq/pull/203)

* Support for the `pennylane.operation.Tensor` observable is removed. This observable was deprecated
in PennyLane 0.39, and is removed in PennyLane 0.40.
[(#204)](https://github.com/PennyLaneAI/pennylane-cirq/pull/204)

### Deprecations 👋

### Documentation 📝
Expand Down
6 changes: 2 additions & 4 deletions pennylane_cirq/cirq_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import cirq
import pennylane as qml
from pennylane.devices import QubitDevice
from pennylane.operation import Tensor
from pennylane.ops import Prod
from pennylane.wires import Wires

Expand Down Expand Up @@ -192,9 +191,8 @@ def supports_operation(self, operation):

def to_paulistring(self, observable):
"""Convert an observable to a cirq.PauliString"""
if isinstance(observable, (Tensor, Prod)):
obs = observable.obs if isinstance(observable, Tensor) else observable.operands
obs = [self.to_paulistring(o) for o in obs]
if isinstance(observable, Prod):
obs = [self.to_paulistring(o) for o in observable.operands]
return functools.reduce(operator.mul, obs)
cirq_op = self._observable_map[observable.name]
if cirq_op is None:
Expand Down
10 changes: 1 addition & 9 deletions pennylane_cirq/qsim_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,8 @@ def observables(self):
return set(self._base_observable_map)

def expval(self, observable, shot_range=None, bin_size=None):
is_tensor = isinstance(observable, (qml.operation.Tensor, qml.ops.Prod))

ob_names = (
[obs.name for obs in observable.operands]
if isinstance(observable, qml.ops.Prod)
else observable.name
)
if (
is_tensor and all(obs == "Identity" for obs in ob_names)
) or observable.name == "Identity":
if isinstance(observable.simplify(), qml.Identity):
return 1

return super().expval(observable, shot_range, bin_size)
Expand Down
57 changes: 18 additions & 39 deletions pennylane_cirq/simulator_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,49 +155,28 @@ def expval(self, observable, shot_range=None, bin_size=None):
# pylint: disable=missing-function-docstring
# Analytic mode
if self.shots is None:
if not isinstance(observable, (qml.operation.Tensor, qml.ops.Prod)):
# Observable on a single wire
# Projector, Hermitian
if self._observable_map[observable.name] is None or observable.name == "Projector":
all_observables = (
list(observable.operands) if isinstance(observable, qml.ops.Prod) else [observable]
)

for obs in all_observables:
if self._observable_map[obs.name] is None or obs.name == "Projector":
return super().expval(observable, shot_range, bin_size)

if observable.name == "Hadamard":
circuit = self.circuit
obs = cirq.PauliSum() + self.to_paulistring(qml.PauliZ(wires=observable.wires))
else:
circuit = self.pre_rotated_circuit
obs = cirq.PauliSum() + self.to_paulistring(observable)
if "Hadamard" in [op.name for op in all_observables]:
list_obs = []

# Observables are in tensor form
else:
for obs in all_observables:
list_obs.append(qml.PauliZ(wires=obs.wires))

ob_names = (
[op.name for op in observable.operands]
if isinstance(observable, qml.ops.Prod)
else observable.name
)

# Projector, Hamiltonian, Hermitian
for name in ob_names:
if self._observable_map[name] is None or name == "Projector":
return super().expval(observable, shot_range, bin_size)

if "Hadamard" in ob_names:
list_obs = []
observables = (
observable.operands
if isinstance(observable, qml.ops.Prod)
else observable.obs
)
for obs in observables:
list_obs.append(qml.PauliZ(wires=obs.wires))

T = qml.prod(*list_obs)
circuit = self.circuit
obs = cirq.PauliSum() + self.to_paulistring(T)
else:
circuit = self.pre_rotated_circuit
obs = cirq.PauliSum() + self.to_paulistring(observable)
T = qml.prod(*list_obs)

circuit = self.circuit
obs = cirq.PauliSum() + self.to_paulistring(T)

else:
circuit = self.pre_rotated_circuit
obs = cirq.PauliSum() + self.to_paulistring(observable)

return self._simulator.simulate_expectation_values(
program=circuit,
Expand Down

0 comments on commit 21c6cf8

Please sign in to comment.