-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Analytic true hw simulator warning (#59)
* Adding basic test cases and changing character setting (need polishing) * Polish tests * Add tests for the variance case * Linting elif statement * Correcting import order * Organize warning message as a class attribute * Reorganized tests of old non-native PL operations, added test for QubitUnitary transformation (ch gate) * Revert "Reorganized tests of old non-native PL operations, added test for QubitUnitary transformation (ch gate)" This reverts commit 7bb7641.
- Loading branch information
1 parent
34976bc
commit 1bab8ea
Showing
4 changed files
with
150 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,116 @@ | ||
import pytest | ||
|
||
import numpy as np | ||
|
||
from pennylane_qiskit.qiskit_device import pauli_eigs | ||
from pennylane_qiskit import AerDevice | ||
|
||
|
||
Z = np.diag([1, -1]) | ||
|
||
|
||
class TestZEigs: | ||
r"""Test that eigenvalues of Z^{\otimes n} are correctly generated""" | ||
|
||
def test_one(self): | ||
"""Test that eigs(Z) = [1, -1]""" | ||
assert np.all(pauli_eigs(1) == np.array([1, -1])) | ||
|
||
@pytest.mark.parametrize("n", [2, 3, 6]) | ||
def test_multiple(self, n): | ||
r"""Test that eigs(Z^{\otimes n}) is correct""" | ||
res = pauli_eigs(n) | ||
Zn = np.kron(Z, Z) | ||
|
||
for _ in range(n - 2): | ||
Zn = np.kron(Zn, Z) | ||
|
||
expected = np.diag(Zn) | ||
assert np.all(res == expected) | ||
|
||
|
||
class TestProbabilities: | ||
"""Tests for the probability function""" | ||
|
||
def test_probability_no_results(self): | ||
"""Test that the probabilities function returns | ||
None if no job has yet been run.""" | ||
dev = AerDevice(backend="statevector_simulator", wires=1, analytic=True) | ||
assert dev.probabilities() is None | ||
import numpy as np | ||
import pytest | ||
|
||
import pennylane as qml | ||
from pennylane_qiskit import AerDevice | ||
from pennylane_qiskit.qiskit_device import pauli_eigs | ||
|
||
Z = np.diag([1, -1]) | ||
|
||
|
||
class TestZEigs: | ||
r"""Test that eigenvalues of Z^{\otimes n} are correctly generated""" | ||
|
||
def test_one(self): | ||
"""Test that eigs(Z) = [1, -1]""" | ||
assert np.all(pauli_eigs(1) == np.array([1, -1])) | ||
|
||
@pytest.mark.parametrize("n", [2, 3, 6]) | ||
def test_multiple(self, n): | ||
r"""Test that eigs(Z^{\otimes n}) is correct""" | ||
res = pauli_eigs(n) | ||
Zn = np.kron(Z, Z) | ||
|
||
for _ in range(n - 2): | ||
Zn = np.kron(Zn, Z) | ||
|
||
expected = np.diag(Zn) | ||
assert np.all(res == expected) | ||
|
||
|
||
class TestProbabilities: | ||
"""Tests for the probability function""" | ||
|
||
def test_probability_no_results(self): | ||
"""Test that the probabilities function returns | ||
None if no job has yet been run.""" | ||
dev = AerDevice(backend="statevector_simulator", wires=1, analytic=True) | ||
assert dev.probabilities() is None | ||
|
||
|
||
class TestAnalyticWarningHWSimulator: | ||
"""Tests the warnings for when the analytic attribute of a device is set to true""" | ||
|
||
def test_warning_raised_for_hardware_backend_analytic_expval(self, hardware_backend, recorder): | ||
"""Tests that a warning is raised if the analytic attribute is true on | ||
hardware simulators when calculating the expectation""" | ||
|
||
dev = qml.device("qiskit.basicaer", backend=hardware_backend, wires=2) | ||
|
||
@qml.qnode(dev) | ||
def circuit(): | ||
qml.Hadamard(wires=0) | ||
return qml.expval(qml.PauliZ(0)) | ||
|
||
with pytest.warns(UserWarning) as record: | ||
circuit() | ||
|
||
# check that only one warning was raised | ||
assert len(record) == 1 | ||
# check that the message matches | ||
assert record[0].message.args[0] == "The analytic calculation of expectations and variances "\ | ||
"is only supported on statevector backends, not on the {}. "\ | ||
"The obtained result is based on sampling.".format(dev.backend) | ||
|
||
def test_no_warning_raised_for_software_backend_analytic_expval(self, statevector_backend, recorder, recwarn): | ||
"""Tests that no warning is raised if the analytic attribute is true on | ||
statevector simulators when calculating the expectation""" | ||
|
||
dev = qml.device("qiskit.basicaer", backend=statevector_backend, wires=2) | ||
|
||
@qml.qnode(dev) | ||
def circuit(): | ||
qml.Hadamard(wires=0) | ||
return qml.expval(qml.PauliZ(0)) | ||
|
||
circuit() | ||
|
||
# check that no warnings were raised | ||
assert len(recwarn) == 0 | ||
|
||
def test_warning_raised_for_hardware_backend_analytic_var(self, hardware_backend, recorder): | ||
"""Tests that a warning is raised if the analytic attribute is true on | ||
hardware simulators when calculating the variance""" | ||
|
||
dev = qml.device("qiskit.basicaer", backend=hardware_backend, wires=2) | ||
|
||
@qml.qnode(dev) | ||
def circuit(): | ||
qml.Hadamard(wires=0) | ||
return qml.var(qml.PauliZ(0)) | ||
|
||
with pytest.warns(UserWarning) as record: | ||
circuit() | ||
|
||
# check that only one warning was raised | ||
assert len(record) == 1 | ||
# check that the message matches | ||
assert record[0].message.args[0] == "The analytic calculation of expectations and variances "\ | ||
"is only supported on statevector backends, not on the {}. "\ | ||
"The obtained result is based on sampling.".format(dev.backend) | ||
|
||
def test_no_warning_raised_for_software_backend_analytic_var(self, statevector_backend, recorder, recwarn): | ||
"""Tests that no warning is raised if the analytic attribute is true on | ||
statevector simulators when calculating the variance""" | ||
|
||
dev = qml.device("qiskit.basicaer", backend=statevector_backend, wires=2) | ||
|
||
@qml.qnode(dev) | ||
def circuit(): | ||
qml.Hadamard(wires=0) | ||
return qml.var(qml.PauliZ(0)) | ||
|
||
circuit() | ||
|
||
# check that no warnings were raised | ||
assert len(recwarn) == 0 |