-
Notifications
You must be signed in to change notification settings - Fork 66
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
Qiskit version 0.14 #65
Changes from 5 commits
0287a00
a54c4cb
b02663d
508b19f
5ccca46
2fc4502
ce187a6
a8b801e
a7a1e23
cf02df2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ | |
from qiskit.compiler import assemble, transpile | ||
from qiskit.converters import circuit_to_dag, dag_to_circuit | ||
|
||
from pennylane import Device | ||
from pennylane import Device, QuantumFunctionError | ||
from pennylane.operation import Sample | ||
|
||
from ._version import __version__ | ||
|
@@ -205,6 +205,9 @@ def apply(self, operation, wires, par): | |
|
||
if operation == "QubitStateVector": | ||
|
||
if self.backend_name == "unitary_simulator": | ||
raise QuantumFunctionError("The QubitStateVector operation is not supported on the unitary simulator backend.") | ||
|
||
if len(par[0]) != 2 ** len(wires): | ||
raise ValueError("State vector must be of length 2**wires.") | ||
|
||
|
@@ -315,7 +318,8 @@ def rotate_basis(self, obs, wires, par): | |
def pre_measure(self): | ||
for e in self.obs_queue: | ||
# Add unitaries if a different expectation value is given | ||
if hasattr(e, "return_type") and e.return_type == Sample: | ||
# Exclude unitary_simulator as it does not support memory=True | ||
if hasattr(e, "return_type") and e.return_type == Sample and self.backend_name != 'unitary_simulator': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Backend support for memory=True now checked when that kwarg is passed. QiskitError results if not supported." - from the release notes of version 0.14.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 |
||
self.memory = True # make sure to return samples | ||
|
||
if isinstance(e.name, list): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,10 @@ class TestStateApply: | |
def test_qubit_state_vector(self, init_state, device, tol): | ||
"""Test PauliX application""" | ||
dev = device(1) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good! For maintainablity, I would suggest moving this into a fixture (e.g., |
||
|
||
state = init_state(1) | ||
|
||
dev.apply("QubitStateVector", [0], [state]) | ||
|
@@ -82,6 +86,10 @@ def test_invalid_qubit_state_vector(self, device): | |
"""Test that an exception is raised if the state | ||
vector is the wrong size""" | ||
dev = device(2) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = np.array([0, 123.432]) | ||
|
||
with pytest.raises(ValueError, match=r"State vector must be of length 2\*\*wires"): | ||
|
@@ -91,6 +99,10 @@ def test_invalid_qubit_state_vector(self, device): | |
def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol): | ||
"""Test PauliX application""" | ||
dev = device(1) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(1) | ||
|
||
dev.apply("QubitStateVector", [0], [state]) | ||
|
@@ -102,11 +114,28 @@ def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol): | |
expected = np.abs(mat @ state) ** 2 | ||
assert np.allclose(res, expected, **tol) | ||
|
||
@pytest.mark.parametrize("name,mat", single_qubit) | ||
def test_qubit_state_vector_error_for_unitary_simulator(self, init_state, device, name, mat, tol): | ||
"""Test PauliX application""" | ||
dev = device(1) | ||
|
||
if dev.backend_name != "unitary_simulator": | ||
pytest.skip("Test only runs for unitary simulator backend.") | ||
|
||
state = init_state(1) | ||
|
||
with pytest.raises(qml.QuantumFunctionError, match="The QubitStateVector operation is not supported on the unitary simulator backend."): | ||
dev.apply("QubitStateVector", [0], [state]) | ||
|
||
@pytest.mark.parametrize("theta", [0.5432, -0.232]) | ||
@pytest.mark.parametrize("name,func", single_qubit_param) | ||
def test_single_qubit_parameters(self, init_state, device, name, func, theta, tol): | ||
"""Test PauliX application""" | ||
dev = device(1) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(1) | ||
|
||
dev.apply("QubitStateVector", [0], [state]) | ||
|
@@ -122,6 +151,10 @@ def test_single_qubit_parameters(self, init_state, device, name, func, theta, to | |
def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol): | ||
"""Test PauliX application""" | ||
dev = device(2) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(2) | ||
|
||
dev.apply("QubitStateVector", [0, 1], [state]) | ||
|
@@ -137,6 +170,10 @@ def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol): | |
def test_qubit_unitary(self, init_state, device, mat, tol): | ||
N = int(np.log2(len(mat))) | ||
dev = device(N) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(N) | ||
|
||
dev.apply("QubitStateVector", list(range(N)), [state]) | ||
|
@@ -160,6 +197,10 @@ def test_invalid_qubit_state_unitary(self, device): | |
@pytest.mark.parametrize("name, mat", three_qubit) | ||
def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol): | ||
dev = device(3) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(3) | ||
|
||
dev.apply("QubitStateVector", [0, 1, 2], [state]) | ||
|
@@ -176,6 +217,10 @@ def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol): | |
def test_single_qubit_parameters(self, init_state, device, name, func, theta, tol): | ||
"""Test PauliX application""" | ||
dev = device(2) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(2) | ||
|
||
dev.apply("QubitStateVector", [0, 1], [state]) | ||
|
@@ -196,6 +241,10 @@ class TestHardwareApply: | |
def test_qubit_state_vector(self, init_state, device, tol): | ||
"""Test PauliX application""" | ||
dev = device(1) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(1) | ||
|
||
dev.apply("QubitStateVector", [0], [state]) | ||
|
@@ -210,6 +259,10 @@ def test_invalid_qubit_state_vector(self, device): | |
"""Test that an exception is raised if the state | ||
vector is the wrong size""" | ||
dev = device(2) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = np.array([0, 123.432]) | ||
|
||
with pytest.raises(ValueError, match=r"State vector must be of length 2\*\*wires"): | ||
|
@@ -219,6 +272,10 @@ def test_invalid_qubit_state_vector(self, device): | |
def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol): | ||
"""Test PauliX application""" | ||
dev = device(1) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(1) | ||
|
||
dev.apply("QubitStateVector", [0], [state]) | ||
|
@@ -235,6 +292,10 @@ def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol): | |
def test_single_qubit_parameters(self, init_state, device, name, func, theta, tol): | ||
"""Test PauliX application""" | ||
dev = device(1) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(1) | ||
|
||
dev.apply("QubitStateVector", [0], [state]) | ||
|
@@ -250,6 +311,10 @@ def test_single_qubit_parameters(self, init_state, device, name, func, theta, to | |
def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol): | ||
"""Test PauliX application""" | ||
dev = device(2) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(2) | ||
|
||
dev.apply("QubitStateVector", [0, 1], [state]) | ||
|
@@ -265,6 +330,10 @@ def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol): | |
def test_qubit_unitary(self, init_state, device, mat, tol): | ||
N = int(np.log2(len(mat))) | ||
dev = device(N) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(N) | ||
|
||
dev.apply("QubitStateVector", list(range(N)), [state]) | ||
|
@@ -288,6 +357,10 @@ def test_invalid_qubit_state_unitary(self, device): | |
@pytest.mark.parametrize("name, mat", three_qubit) | ||
def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol): | ||
dev = device(3) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(3) | ||
|
||
dev.apply("QubitStateVector", [0, 1, 2], [state]) | ||
|
@@ -304,6 +377,10 @@ def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol): | |
def test_single_qubit_parameters(self, init_state, device, name, func, theta, tol): | ||
"""Test PauliX application""" | ||
dev = device(2) | ||
|
||
if dev.backend_name == "unitary_simulator": | ||
pytest.skip("Test only runs for backends that are not the unitary simulator.") | ||
|
||
state = init_state(2) | ||
|
||
dev.apply("QubitStateVector", [0, 1], [state]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines were added as per this issue: Qiskit/qiskit-aer#497
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!