Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add representation to MeasurementProcess #883

Merged
merged 5 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,6 @@ def __init__(self, *params, wires=None, do_queue=True):
if do_queue:
self.queue()

def __str__(self):
"""Operator name and some information."""
return "{}: {} params, wires {}".format(self.name, len(self.data), self.wires.tolist())

def __repr__(self):
"""Constructor-call-like representation."""
# FIXME using self.parameters here instead of self.data is dangerous, it assumes the data can be evaluated
Expand Down Expand Up @@ -1188,15 +1184,18 @@ def __copy__(self):
copied_op._eigvals_cache = self._eigvals_cache
return copied_op

def __str__(self):
"""Print the tensor product and some information."""
return "Tensor product {}: {} params, wires {}".format(
[i.name for i in self.obs], len(self.data), self.wires.tolist()
)

def __repr__(self):
"""Constructor-call-like representation."""
return "Tensor(" + ", ".join([repr(o) for o in self.obs]) + ")"

s = " @ ".join([repr(o) for o in self.obs])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯


if self.return_type is None:
return s

if self.return_type is Probability:
return repr(self.return_type) + "(wires={})".format(self.wires.tolist())

return repr(self.return_type) + "(" + s + ")"

@property
def name(self):
Expand Down
10 changes: 10 additions & 0 deletions pennylane/tape/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ def __init__(self, return_type, obs=None, wires=None, eigvals=None):
# Queue the measurement process
self.queue()

def __repr__(self):
"""Representation of this class."""
if self.obs is None:
return "{}(None)".format(self.return_type.value)
mariaschuld marked this conversation as resolved.
Show resolved Hide resolved

if self.obs.return_type is None:
mariaschuld marked this conversation as resolved.
Show resolved Hide resolved
return "{}({})".format(self.return_type.value, self.obs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the return type is not none, it was already added in the __repr__ of the observable.

I am not sure if this has to do with the two cores? Shouldn't the return type be only specified in the process? I tried not to change too much here...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this has to do with the two cores? Shouldn't the return type be only specified in the process? I tried not to change too much here...

Yep, this is to support non-tape mode, where observables are still annotated directly with the return type.

Perhaps we should add a TODO to this method, to remind us to fix it when tape mode is default?


return "{}".format(self.obs)

def __copy__(self):
cls = self.__class__

Expand Down
10 changes: 10 additions & 0 deletions tests/tape/test_tape_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ def test_observable_with_no_eigvals(self):
m = MeasurementProcess(Expectation, obs=obs)
assert m.eigvals is None

def test_repr(self):
"""Test the string representation of a MeasurementProcess."""
m = MeasurementProcess(Expectation, obs=qml.PauliZ(wires='a') @ qml.PauliZ(wires='b'))
expected = "expval(PauliZ(wires=['a']) @ PauliZ(wires=['b']))"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

assert str(m) == expected

m = MeasurementProcess(Probability, obs=qml.PauliZ(wires='a'))
expected = "probs(PauliZ(wires=['a']))"
assert str(m) == expected


class TestExpansion:
"""Test for measurement expansion"""
Expand Down
6 changes: 0 additions & 6 deletions tests/test_observable.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@
Unit tests for the :mod:`pennylane.plugin.DefaultGaussian` device.
"""
# pylint: disable=protected-access,cell-var-from-loop
from pennylane import numpy as np
from scipy.linalg import block_diag

import pennylane as qml
from pennylane.qnodes import QuantumFunctionError
from pennylane.devices import DefaultQubit

import pytest


def test_pass_positional_wires_to_observable():
Expand Down
19 changes: 19 additions & 0 deletions tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,25 @@ def test_tensor_n_single_mode_wires_implicit(self):
assert cv_obs.wires == Wires([1])
assert cv_obs.ev_order == 2

def test_repr(self):
"""Test the string representation of an observable with and without a return type."""

m = qml.expval(qml.PauliZ(wires=['a']) @ qml.PauliZ(wires=['b']))
expected = "expval(PauliZ(wires=['a']) @ PauliZ(wires=['b']))"
assert str(m) == expected

m = qml.probs(wires=['a'])
expected = "probs(wires=['a'])"
assert str(m) == expected

m = qml.PauliZ(wires=['a']) @ qml.PauliZ(wires=['b'])
expected = "PauliZ(wires=['a']) @ PauliZ(wires=['b'])"
assert str(m) == expected

m = qml.PauliZ(wires=['a'])
expected = "PauliZ(wires=['a'])"
assert str(m) == expected


class TestOperatorIntegration:
""" Integration tests for the Operator class"""
Expand Down