From 84f7c4354a598f4ecd6977f7d2116b98296ecc9f Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Tue, 12 Apr 2022 12:13:28 -0400 Subject: [PATCH 1/7] Add raising an error --- pennylane/measurements.py | 7 +++++++ tests/test_prob.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pennylane/measurements.py b/pennylane/measurements.py index d287e76300c..0f716bcc3c2 100644 --- a/pennylane/measurements.py +++ b/pennylane/measurements.py @@ -133,6 +133,7 @@ def diagonalizing_gates(self): List[.Operation]: the operations that diagonalize the observables """ try: + # pylint: disable=no-member return self.expand().operations except qml.operation.DecompositionUndefinedError: return [] @@ -517,6 +518,12 @@ def circuit(): the computational basis """ # pylint: disable=protected-access + + if wires is None and op is None: + raise qml.QuantumFunctionError( + "qml.probs requires either the wires or the observable to be passed." + ) + if isinstance(op, qml.Hamiltonian): raise qml.QuantumFunctionError("Hamiltonians are not supported for rotating probabilities.") diff --git a/tests/test_prob.py b/tests/test_prob.py index b9ad7222c64..8a0f2e36a28 100644 --- a/tests/test_prob.py +++ b/tests/test_prob.py @@ -437,6 +437,24 @@ def circuit(): circuit() +def test_probs_no_wires_obs_raises(): + "Test that an error is returned for hamiltonians." + num_wires = 1 + + dev = qml.device("default.qubit", wires=num_wires, shots=None) + + @qml.qnode(dev) + def circuit_probs(): + qml.RY(0.34, wires=0) + return qml.probs() + + with pytest.raises( + qml.QuantumFunctionError, + match="qml.probs requires either the wires or the observable to be passed.", + ): + circuit_probs() + + @pytest.mark.parametrize( "operation", [qml.SingleExcitation, qml.SingleExcitationPlus, qml.SingleExcitationMinus] ) From 8594ecf490ab4ac830eb931f7da31584c4e644d6 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Tue, 12 Apr 2022 12:16:03 -0400 Subject: [PATCH 2/7] changelog --- doc/releases/changelog-dev.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 27b1f40c16b..60d3f93a202 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -366,6 +366,10 @@ the `decimals` and `show_matrices` keywords are added. `qml.drawer.tape_text(tap

Bug fixes

+* Fixes a bug by adding a comprehensible error message for calling `qml.probs` + without passing wires or an observable. + [(#2438)](https://github.com/PennyLaneAI/pennylane/pull/2438) + * Call `pip show` with the subprocess library to avoid outputting a common warning. [(#2422)](https://github.com/PennyLaneAI/pennylane/pull/2422) From f5df4306f8534c8f50fd385bb60803f6beb6abd8 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Tue, 12 Apr 2022 13:08:42 -0400 Subject: [PATCH 3/7] docstring --- tests/test_prob.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_prob.py b/tests/test_prob.py index 8a0f2e36a28..8eaecf253f9 100644 --- a/tests/test_prob.py +++ b/tests/test_prob.py @@ -438,7 +438,8 @@ def circuit(): def test_probs_no_wires_obs_raises(): - "Test that an error is returned for hamiltonians." + """Test that an informative error is raised when no wires or observable are + passed to qml.probs.""" num_wires = 1 dev = qml.device("default.qubit", wires=num_wires, shots=None) From d818385248c74bdb242ffa8ce8a5540fe5376ed7 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Tue, 12 Apr 2022 13:09:19 -0400 Subject: [PATCH 4/7] codecov action version --- .github/workflows/tests.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 20f2ab64b3a..42bcbaf64ec 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -79,7 +79,7 @@ jobs: run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v2 with: file: ./coverage.xml @@ -146,7 +146,7 @@ jobs: run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v2 with: file: ./coverage.xml @@ -165,7 +165,7 @@ jobs: fetch-depth: 2 - name: Setup conda - uses: s-weigand/setup-conda@v1 + uses: s-weigand/setup-conda@v2 with: activate-conda: true python-version: 3.7 @@ -191,11 +191,11 @@ jobs: cd qchem && python -m pytest tests --cov=pennylane_qchem $COVERAGE_FLAGS - name: Upload HF coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v2 with: file: ./coverage.xml - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v2 with: file: ./qchem/coverage.xml From 24248cdc377fc7db9a52da180ab1a0849c827558 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Tue, 12 Apr 2022 13:10:27 -0400 Subject: [PATCH 5/7] docstring --- tests/test_prob.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_prob.py b/tests/test_prob.py index 8eaecf253f9..7a726732230 100644 --- a/tests/test_prob.py +++ b/tests/test_prob.py @@ -438,8 +438,8 @@ def circuit(): def test_probs_no_wires_obs_raises(): - """Test that an informative error is raised when no wires or observable are - passed to qml.probs.""" + """Test that an informative error is raised when no wires or observables + are passed to qml.probs.""" num_wires = 1 dev = qml.device("default.qubit", wires=num_wires, shots=None) From 59d5aeed7ad85ad3424cc4ba48f9c9ce0e271d0b Mon Sep 17 00:00:00 2001 From: Romain Moyard Date: Tue, 12 Apr 2022 20:19:59 +0200 Subject: [PATCH 6/7] Update .github/workflows/tests.yml --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 42bcbaf64ec..17d07bd4333 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -165,7 +165,7 @@ jobs: fetch-depth: 2 - name: Setup conda - uses: s-weigand/setup-conda@v2 + uses: s-weigand/setup-conda@v1 with: activate-conda: true python-version: 3.7 From bee8ac93eb35ada8b00c235421a7be23c74cfd88 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Tue, 12 Apr 2022 18:07:49 -0400 Subject: [PATCH 7/7] Trigger CI