From 0fdcd40002114d3b2257115fa8dc6f8933868fd6 Mon Sep 17 00:00:00 2001 From: Maria Schuld Date: Fri, 26 Feb 2021 11:34:20 +0200 Subject: [PATCH 1/9] fix --- pennylane/devices/default_qubit_tf.py | 2 +- pennylane/templates/embeddings/basis.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pennylane/devices/default_qubit_tf.py b/pennylane/devices/default_qubit_tf.py index c80f20e0e94..d3c9cae9d6a 100644 --- a/pennylane/devices/default_qubit_tf.py +++ b/pennylane/devices/default_qubit_tf.py @@ -120,7 +120,7 @@ class DefaultQubitTF(DefaultQubit): shots (int): How many times the circuit should be evaluated (or sampled) to estimate the expectation values. Defaults to 1000 if not specified. If ``analytic == True``, then the number of shots is ignored - in the calculation of expectation values and variances, and only controls the number + in the calcifulation of expectation values and variances, and only controls the number of samples returned by ``sample``. analytic (bool): Indicates if the device should calculate expectations and variances analytically. In non-analytic mode, the ``diff_method="backprop"`` diff --git a/pennylane/templates/embeddings/basis.py b/pennylane/templates/embeddings/basis.py index e06a5031886..c8d3c4b7427 100644 --- a/pennylane/templates/embeddings/basis.py +++ b/pennylane/templates/embeddings/basis.py @@ -51,7 +51,7 @@ def _preprocess(features, wires): features = list(qml.math.toarray(features)) - if set(features) != {0, 1}: + if any(b not in [0, 1] for b in set(features)): raise ValueError(f"Basis state must only consist of 0s and 1s; got {features}") return features From d9d7f8c7cedbfdae725745d67dc136824baded7f Mon Sep 17 00:00:00 2001 From: Maria Schuld Date: Fri, 26 Feb 2021 11:36:56 +0200 Subject: [PATCH 2/9] reverse accidental change --- pennylane/devices/default_qubit_tf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane/devices/default_qubit_tf.py b/pennylane/devices/default_qubit_tf.py index d3c9cae9d6a..c80f20e0e94 100644 --- a/pennylane/devices/default_qubit_tf.py +++ b/pennylane/devices/default_qubit_tf.py @@ -120,7 +120,7 @@ class DefaultQubitTF(DefaultQubit): shots (int): How many times the circuit should be evaluated (or sampled) to estimate the expectation values. Defaults to 1000 if not specified. If ``analytic == True``, then the number of shots is ignored - in the calcifulation of expectation values and variances, and only controls the number + in the calculation of expectation values and variances, and only controls the number of samples returned by ``sample``. analytic (bool): Indicates if the device should calculate expectations and variances analytically. In non-analytic mode, the ``diff_method="backprop"`` From c18a64c908f19419030eae51025e758c86d5c9ba Mon Sep 17 00:00:00 2001 From: Maria Schuld Date: Fri, 26 Feb 2021 11:39:02 +0200 Subject: [PATCH 3/9] simplify solution --- pennylane/templates/embeddings/basis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane/templates/embeddings/basis.py b/pennylane/templates/embeddings/basis.py index c8d3c4b7427..2a672bf7bdc 100644 --- a/pennylane/templates/embeddings/basis.py +++ b/pennylane/templates/embeddings/basis.py @@ -51,7 +51,7 @@ def _preprocess(features, wires): features = list(qml.math.toarray(features)) - if any(b not in [0, 1] for b in set(features)): + if any(b not in [0, 1] for b in features): raise ValueError(f"Basis state must only consist of 0s and 1s; got {features}") return features From 5293dde8a9cdd3e5d4ba2243a38150b11b7421cc Mon Sep 17 00:00:00 2001 From: Maria Schuld Date: Fri, 26 Feb 2021 11:41:21 +0200 Subject: [PATCH 4/9] update changelog --- .github/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 63cc5c1c62d..48802899840 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -95,6 +95,11 @@

Bug fixes

+* `BasisEmbedding` does now accept inputs whose bits are all ones + or all zeros. + [(#1114)](https://github.com/PennyLaneAI/pennylane/pull/1114) + + * The `ExpvalCost` class raises an error if instantiated with non-expectation measurement statistics. [(#1106)](https://github.com/PennyLaneAI/pennylane/pull/1106) From 5c7ead6492334a0417ec79ae5984614be071ab61 Mon Sep 17 00:00:00 2001 From: Maria Schuld Date: Fri, 26 Feb 2021 11:41:51 +0200 Subject: [PATCH 5/9] Update .github/CHANGELOG.md --- .github/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 48802899840..3ecb1098b1d 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -99,7 +99,6 @@ or all zeros. [(#1114)](https://github.com/PennyLaneAI/pennylane/pull/1114) - * The `ExpvalCost` class raises an error if instantiated with non-expectation measurement statistics. [(#1106)](https://github.com/PennyLaneAI/pennylane/pull/1106) From 216d3890a06aa2da5c39e9cad91c7d516d9c33e2 Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Mon, 1 Mar 2021 00:01:28 +0800 Subject: [PATCH 6/9] Update .github/CHANGELOG.md --- .github/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 3ecb1098b1d..b114c1860ed 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -95,8 +95,8 @@

Bug fixes

-* `BasisEmbedding` does now accept inputs whose bits are all ones - or all zeros. +* Fixes a bug where `BasisEmbedding` would not accept inputs whose bits are all ones + or all zeros. [(#1114)](https://github.com/PennyLaneAI/pennylane/pull/1114) * The `ExpvalCost` class raises an error if instantiated From eccedbbaa5875590baf66e95309fe14ef4b4de4b Mon Sep 17 00:00:00 2001 From: Maria Schuld Date: Mon, 1 Mar 2021 07:05:05 +0200 Subject: [PATCH 7/9] Update pennylane/templates/embeddings/basis.py Co-authored-by: Josh Izaac --- pennylane/templates/embeddings/basis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane/templates/embeddings/basis.py b/pennylane/templates/embeddings/basis.py index 2a672bf7bdc..8e51a4797a4 100644 --- a/pennylane/templates/embeddings/basis.py +++ b/pennylane/templates/embeddings/basis.py @@ -51,7 +51,7 @@ def _preprocess(features, wires): features = list(qml.math.toarray(features)) - if any(b not in [0, 1] for b in features): + if set(features).issubset({0, 1}): raise ValueError(f"Basis state must only consist of 0s and 1s; got {features}") return features From d46b2d5a5214da9add2f1a8da1697ee220e82a0d Mon Sep 17 00:00:00 2001 From: Maria Schuld Date: Mon, 1 Mar 2021 07:09:44 +0200 Subject: [PATCH 8/9] add test --- tests/templates/test_embeddings.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/templates/test_embeddings.py b/tests/templates/test_embeddings.py index 1fe880ef212..986bc7b8606 100644 --- a/tests/templates/test_embeddings.py +++ b/tests/templates/test_embeddings.py @@ -332,10 +332,13 @@ def circuit(x=None): class TestBasisEmbedding: """ Tests the BasisEmbedding method.""" - def test_state(self): - """Checks the state.""" + @pytest.mark.parametrize("state", [[0, 1], + [1, 1], + [1, 0], + [0, 0]]) + def test_state(self, state): + """Checks that the correct state is prepared.""" - state = np.array([0, 1]) n_qubits = 2 dev = qml.device('default.qubit', wires=n_qubits) @@ -345,7 +348,8 @@ def circuit(x=None): return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)] res = circuit(x=state) - assert np.allclose(res, [1, -1]) + expected = [1 if s == 0 else -1 for s in state] + assert np.allclose(res, expected) def test_too_many_input_bits_exception(self): """Verifies that exception thrown if there are more features than qubits.""" From 7b581062e1de2d4634e365d346b6fa17222d97a1 Mon Sep 17 00:00:00 2001 From: antalszava Date: Tue, 2 Mar 2021 09:04:29 -0500 Subject: [PATCH 9/9] Update pennylane/templates/embeddings/basis.py --- pennylane/templates/embeddings/basis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane/templates/embeddings/basis.py b/pennylane/templates/embeddings/basis.py index 8e51a4797a4..a377a50128e 100644 --- a/pennylane/templates/embeddings/basis.py +++ b/pennylane/templates/embeddings/basis.py @@ -51,7 +51,7 @@ def _preprocess(features, wires): features = list(qml.math.toarray(features)) - if set(features).issubset({0, 1}): + if not set(features).issubset({0, 1}): raise ValueError(f"Basis state must only consist of 0s and 1s; got {features}") return features