Skip to content

Commit 21c6cf8

Browse files
authored
Remove Tensor in devices (#204)
* 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
1 parent e94c315 commit 21c6cf8

File tree

4 files changed

+25
-52
lines changed

4 files changed

+25
-52
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* The ``qml.QubitStateVector`` template has been removed. Instead, use :class:`~pennylane.StatePrep`.
1010
[(#203)](https://github.com/PennyLaneAI/pennylane-cirq/pull/203)
1111

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

1418
### Documentation 📝

pennylane_cirq/cirq_device.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import cirq
4242
import pennylane as qml
4343
from pennylane.devices import QubitDevice
44-
from pennylane.operation import Tensor
4544
from pennylane.ops import Prod
4645
from pennylane.wires import Wires
4746

@@ -192,9 +191,8 @@ def supports_operation(self, operation):
192191

193192
def to_paulistring(self, observable):
194193
"""Convert an observable to a cirq.PauliString"""
195-
if isinstance(observable, (Tensor, Prod)):
196-
obs = observable.obs if isinstance(observable, Tensor) else observable.operands
197-
obs = [self.to_paulistring(o) for o in obs]
194+
if isinstance(observable, Prod):
195+
obs = [self.to_paulistring(o) for o in observable.operands]
198196
return functools.reduce(operator.mul, obs)
199197
cirq_op = self._observable_map[observable.name]
200198
if cirq_op is None:

pennylane_cirq/qsim_device.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,8 @@ def observables(self):
8181
return set(self._base_observable_map)
8282

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

86-
ob_names = (
87-
[obs.name for obs in observable.operands]
88-
if isinstance(observable, qml.ops.Prod)
89-
else observable.name
90-
)
91-
if (
92-
is_tensor and all(obs == "Identity" for obs in ob_names)
93-
) or observable.name == "Identity":
85+
if isinstance(observable.simplify(), qml.Identity):
9486
return 1
9587

9688
return super().expval(observable, shot_range, bin_size)

pennylane_cirq/simulator_device.py

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -155,49 +155,28 @@ def expval(self, observable, shot_range=None, bin_size=None):
155155
# pylint: disable=missing-function-docstring
156156
# Analytic mode
157157
if self.shots is None:
158-
if not isinstance(observable, (qml.operation.Tensor, qml.ops.Prod)):
159-
# Observable on a single wire
160-
# Projector, Hermitian
161-
if self._observable_map[observable.name] is None or observable.name == "Projector":
158+
all_observables = (
159+
list(observable.operands) if isinstance(observable, qml.ops.Prod) else [observable]
160+
)
161+
162+
for obs in all_observables:
163+
if self._observable_map[obs.name] is None or obs.name == "Projector":
162164
return super().expval(observable, shot_range, bin_size)
163165

164-
if observable.name == "Hadamard":
165-
circuit = self.circuit
166-
obs = cirq.PauliSum() + self.to_paulistring(qml.PauliZ(wires=observable.wires))
167-
else:
168-
circuit = self.pre_rotated_circuit
169-
obs = cirq.PauliSum() + self.to_paulistring(observable)
166+
if "Hadamard" in [op.name for op in all_observables]:
167+
list_obs = []
170168

171-
# Observables are in tensor form
172-
else:
169+
for obs in all_observables:
170+
list_obs.append(qml.PauliZ(wires=obs.wires))
173171

174-
ob_names = (
175-
[op.name for op in observable.operands]
176-
if isinstance(observable, qml.ops.Prod)
177-
else observable.name
178-
)
179-
180-
# Projector, Hamiltonian, Hermitian
181-
for name in ob_names:
182-
if self._observable_map[name] is None or name == "Projector":
183-
return super().expval(observable, shot_range, bin_size)
184-
185-
if "Hadamard" in ob_names:
186-
list_obs = []
187-
observables = (
188-
observable.operands
189-
if isinstance(observable, qml.ops.Prod)
190-
else observable.obs
191-
)
192-
for obs in observables:
193-
list_obs.append(qml.PauliZ(wires=obs.wires))
194-
195-
T = qml.prod(*list_obs)
196-
circuit = self.circuit
197-
obs = cirq.PauliSum() + self.to_paulistring(T)
198-
else:
199-
circuit = self.pre_rotated_circuit
200-
obs = cirq.PauliSum() + self.to_paulistring(observable)
172+
T = qml.prod(*list_obs)
173+
174+
circuit = self.circuit
175+
obs = cirq.PauliSum() + self.to_paulistring(T)
176+
177+
else:
178+
circuit = self.pre_rotated_circuit
179+
obs = cirq.PauliSum() + self.to_paulistring(observable)
201180

202181
return self._simulator.simulate_expectation_values(
203182
program=circuit,

0 commit comments

Comments
 (0)