diff --git a/README.rst b/README.rst index 511dba2..78e5833 100644 --- a/README.rst +++ b/README.rst @@ -60,7 +60,7 @@ Contributing We welcome contributions - simply fork the Plugin Name repository, and then make a `pull request `_ containing your contribution. -All contributers to PennyLane-SF will be listed as authors on the releases. +All contributers to Plugin-Name will be listed as authors on the releases. We also encourage bug reports, suggestions for new features and enhancements, and even links to cool projects or applications built on PennyLane and Target Framework. @@ -86,7 +86,7 @@ Support ======= - **Source Code:** https://github.com/XanaduAI/plugin-name -- **Issue Tracker:** https://github.com/XanaduAI/plugin-namesf/issues +- **Issue Tracker:** https://github.com/XanaduAI/plugin-name/issues If you are having issues, please let us know by posting the issue on our GitHub issue tracker. diff --git a/doc/conf.py b/doc/conf.py index 1740e43..0845949 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -60,9 +60,9 @@ # |version| and |release|, also used in various other places throughout the # built documents. -import pennylane_forest +import plugin_name # The full version, including alpha/beta/rc tags. -release = pennylane_forest.__version__ +release = plugin_name.__version__ # The short X.Y version. version = re.match(r'^(\d+\.\d+)', release).expand(r'\1') diff --git a/plugin_name/ops.py b/plugin_name/ops.py index 3d38ceb..dfc1c96 100644 --- a/plugin_name/ops.py +++ b/plugin_name/ops.py @@ -15,9 +15,9 @@ Custom operations ================= -**Module name:** :mod:`pennylane_forest.ops` +**Module name:** :mod:`plugin_name.ops` -.. currentmodule:: pennylane_forest.ops +.. currentmodule:: plugin_name.ops Sometimes the Target Framework may accept more operations than available by core PennyLane. The plugin can define diff --git a/tests/test_apply.py b/tests/test_apply.py index 0c259d3..afb978d 100755 --- a/tests/test_apply.py +++ b/tests/test_apply.py @@ -37,7 +37,7 @@ SWAP = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) CZ = np.diag([1, 1, 1, -1]) -toffoli = np.diag([1 for i in range(8)]) +toffoli = np.eye(8) toffoli[6:8, 6:8] = np.array([[0, 1], [1, 0]]) CSWAP = block_diag(I, I, SWAP) @@ -88,11 +88,15 @@ def test_basis_state(self, device, tol): dev = device(4) state = np.array([0, 0, 1, 0]) - dev.apply("BasisState", [0, 1, 2, 3], [state]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("BasisState", [0, 1, 2, 3], [state]) + dev.post_apply() - res = np.abs(dev.state) ** 2 + dev._obs_queue = [] + dev.pre_measure() + + res = np.abs(dev.state) ** 2 expected = np.zeros([2 ** 4]) expected[np.ravel_multi_index(state, [2] * 4)] = 1 @@ -103,9 +107,13 @@ def test_identity_basis_state(self, device, tol): dev = device(4) state = np.array([1, 0, 0, 0]) - dev.apply("BasisState", [0, 1, 2, 3], [state]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("BasisState", [0, 1, 2, 3], [state]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 @@ -114,13 +122,17 @@ def test_identity_basis_state(self, device, tol): assert np.allclose(res, expected, **tol) def test_qubit_state_vector(self, init_state, device, tol): - """Test PauliX application""" + """Test initialization from a state vector.""" dev = device(1) state = init_state(1) - dev.apply("QubitStateVector", [0], [state]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0], [state]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 expected = np.abs(state) ** 2 @@ -137,14 +149,18 @@ def test_invalid_qubit_state_vector(self, device): @pytest.mark.parametrize("name,mat", single_qubit) def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol): - """Test PauliX application""" + """Test application of non-parametrized single-qubit gates.""" dev = device(1) state = init_state(1) - dev.apply("QubitStateVector", [0], [state]) - dev.apply(name, [0], []) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0], [state]) + dev.apply(name, [0], []) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 expected = np.abs(mat @ state) ** 2 @@ -153,14 +169,18 @@ def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol): @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""" + """Test application of parametrized single-qubit gates.""" dev = device(1) state = init_state(1) - dev.apply("QubitStateVector", [0], [state]) - dev.apply(name, [0], [theta]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0], [state]) + dev.apply(name, [0], [theta]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 expected = np.abs(func(theta) @ state) ** 2 @@ -175,10 +195,14 @@ def test_rotation(self, init_state, device, tol): b = 1.3432 c = -0.654 - dev.apply("QubitStateVector", [0], [state]) - dev.apply("Rot", [0], [a, b, c]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0], [state]) + dev.apply("Rot", [0], [a, b, c]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 expected = np.abs(rot(a, b, c) @ state) ** 2 @@ -186,14 +210,18 @@ def test_rotation(self, init_state, device, tol): @pytest.mark.parametrize("name,mat", two_qubit) def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol): - """Test PauliX application""" + """Test application of non-parametrized two-qubit gates.""" dev = device(2) state = init_state(2) - dev.apply("QubitStateVector", [0, 1], [state]) - dev.apply(name, [0, 1], []) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0, 1], [state]) + dev.apply(name, [0, 1], []) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 expected = np.abs(mat @ state) ** 2 @@ -201,14 +229,19 @@ def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol): @pytest.mark.parametrize("mat", [U, U2]) def test_qubit_unitary(self, init_state, device, mat, tol): + """Test application of qubit unitaries.""" N = int(np.log2(len(mat))) dev = device(N) state = init_state(N) - dev.apply("QubitStateVector", list(range(N)), [state]) - dev.apply("QubitUnitary", list(range(N)), [mat]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", list(range(N)), [state]) + dev.apply("QubitUnitary", list(range(N)), [mat]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 expected = np.abs(mat @ state) ** 2 @@ -220,18 +253,25 @@ def test_invalid_qubit_state_unitary(self, device): dev = device(2) state = np.array([[0, 123.432], [-0.432, 023.4]]) - with pytest.raises(ValueError, match=r"Unitary matrix must be of shape"): - dev.apply("QubitUnitary", [0, 1], [state]) + with dev.execution_context(): + dev.pre_apply() + with pytest.raises(ValueError, match=r"Unitary matrix must be of shape"): + dev.apply("QubitUnitary", [0, 1], [state]) @pytest.mark.parametrize("name, mat", three_qubit) def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol): + """Test application of non-parametrized three-qubit gates.""" dev = device(3) state = init_state(3) - dev.apply("QubitStateVector", [0, 1, 2], [state]) - dev.apply("QubitUnitary", [0, 1, 2], [mat]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0, 1, 2], [state]) + dev.apply("QubitUnitary", [0, 1, 2], [mat]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 expected = np.abs(mat @ state) ** 2 @@ -240,14 +280,18 @@ def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol): @pytest.mark.parametrize("theta", [0.5432, -0.232]) @pytest.mark.parametrize("name,func", two_qubit_param) def test_single_qubit_parameters(self, init_state, device, name, func, theta, tol): - """Test PauliX application""" + """Test application of parametrized single-qubit gates.""" dev = device(2) state = init_state(2) - dev.apply("QubitStateVector", [0, 1], [state]) - dev.apply(name, [0, 1], [theta]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0, 1], [state]) + dev.apply(name, [0, 1], [theta]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.abs(dev.state) ** 2 expected = np.abs(func(theta) @ state) ** 2 @@ -263,9 +307,13 @@ def test_basis_state(self, device, tol): dev = device(4) state = np.array([0, 0, 1, 0]) - dev.apply("BasisState", [0, 1, 2, 3], [state]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("BasisState", [0, 1, 2, 3], [state]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities(wires=range(4)).values(), dtype=np.float64) @@ -279,21 +327,27 @@ def test_basis_state_not_first_operation(self, device): dev = device(4) state = np.array([0, 0, 1, 0]) - dev.apply("Hadamard", [0], []) + with dev.execution_context(): + dev.pre_apply() + dev.apply("Hadamard", [0], []) - with pytest.raises( - qml.DeviceError, match="annot be used after other Operations have already been applied" - ): - dev.apply("BasisState", [0, 1, 2, 3], [state]) + with pytest.raises( + qml.DeviceError, match="annot be used after other Operations have already been applied" + ): + dev.apply("BasisState", [0, 1, 2, 3], [state]) def test_qubit_state_vector(self, init_state, device, tol): - """Test PauliX application""" + """Test initialization from a state vector.""" dev = device(1) state = init_state(1) - dev.apply("QubitStateVector", [0], [state]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0], [state]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities().values(), dtype=np.float64) expected = np.abs(state) ** 2 @@ -305,19 +359,25 @@ def test_invalid_qubit_state_vector(self, device): dev = device(2) state = np.array([0, 123.432]) - with pytest.raises(ValueError, match=r"State vector must be of length 2\*\*wires"): - dev.apply("QubitStateVector", [0, 1], [state]) + with dev.execution_context(): + dev.pre_apply() + with pytest.raises(ValueError, match=r"State vector must be of length 2\*\*wires"): + dev.apply("QubitStateVector", [0, 1], [state]) @pytest.mark.parametrize("name,mat", single_qubit) def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol): - """Test PauliX application""" + """Test application of non-parametrized single-qubit gates.""" dev = device(1) state = init_state(1) - dev.apply("QubitStateVector", [0], [state]) - dev.apply(name, [0], []) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0], [state]) + dev.apply(name, [0], []) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities().values(), dtype=np.float64) expected = np.abs(mat @ state) ** 2 @@ -326,14 +386,18 @@ def test_single_qubit_no_parameters(self, init_state, device, name, mat, tol): @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""" + """Test application of parametrized single-qubit gates.""" dev = device(1) state = init_state(1) - dev.apply("QubitStateVector", [0], [state]) - dev.apply(name, [0], [theta]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0], [state]) + dev.apply(name, [0], [theta]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities().values(), dtype=np.float64) expected = np.abs(func(theta) @ state) ** 2 @@ -348,10 +412,14 @@ def test_rotation(self, init_state, device, tol): b = 1.3432 c = -0.654 - dev.apply("QubitStateVector", [0], [state]) - dev.apply("Rot", [0], [a, b, c]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0], [state]) + dev.apply("Rot", [0], [a, b, c]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities().values(), dtype=np.float64) expected = np.abs(rot(a, b, c) @ state) ** 2 @@ -359,14 +427,18 @@ def test_rotation(self, init_state, device, tol): @pytest.mark.parametrize("name,mat", two_qubit) def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol): - """Test PauliX application""" + """Test application of non-parametrized two-qubit gates.""" dev = device(2) state = init_state(2) - dev.apply("QubitStateVector", [0, 1], [state]) - dev.apply(name, [0, 1], []) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0, 1], [state]) + dev.apply(name, [0, 1], []) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities().values(), dtype=np.float64) expected = np.abs(mat @ state) ** 2 @@ -374,14 +446,19 @@ def test_two_qubit_no_parameters(self, init_state, device, name, mat, tol): @pytest.mark.parametrize("mat", [U, U2]) def test_qubit_unitary(self, init_state, device, mat, tol): + """Test application of qubit unitaries.""" N = int(np.log2(len(mat))) dev = device(N) state = init_state(N) - dev.apply("QubitStateVector", list(range(N)), [state]) - dev.apply("QubitUnitary", list(range(N)), [mat]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", list(range(N)), [state]) + dev.apply("QubitUnitary", list(range(N)), [mat]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities().values(), dtype=np.float64) expected = np.abs(mat @ state) ** 2 @@ -393,18 +470,25 @@ def test_invalid_qubit_state_unitary(self, device): dev = device(2) state = np.array([[0, 123.432], [-0.432, 023.4]]) - with pytest.raises(ValueError, match=r"Unitary matrix must be of shape"): - dev.apply("QubitUnitary", [0, 1], [state]) + with dev.execution_context(): + dev.pre_apply() + with pytest.raises(ValueError, match=r"Unitary matrix must be of shape"): + dev.apply("QubitUnitary", [0, 1], [state]) @pytest.mark.parametrize("name, mat", three_qubit) def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol): + """Test application of non-parametrized three-qubit gates.""" dev = device(3) state = init_state(3) - dev.apply("QubitStateVector", [0, 1, 2], [state]) - dev.apply("QubitUnitary", [0, 1, 2], [mat]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0, 1, 2], [state]) + dev.apply("QubitUnitary", [0, 1, 2], [mat]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities().values(), dtype=np.float64) expected = np.abs(mat @ state) ** 2 @@ -413,14 +497,18 @@ def test_three_qubit_no_parameters(self, init_state, device, name, mat, tol): @pytest.mark.parametrize("theta", [0.5432, -0.232]) @pytest.mark.parametrize("name,func", two_qubit_param) def test_single_qubit_parameters(self, init_state, device, name, func, theta, tol): - """Test PauliX application""" + """Test application of parameterized single qubit gates.""" dev = device(2) state = init_state(2) - dev.apply("QubitStateVector", [0, 1], [state]) - dev.apply(name, [0, 1], [theta]) - dev._obs_queue = [] - dev.pre_measure() + with dev.execution_context(): + dev.pre_apply() + dev.apply("QubitStateVector", [0, 1], [state]) + dev.apply(name, [0, 1], [theta]) + dev.post_apply() + + dev._obs_queue = [] + dev.pre_measure() res = np.fromiter(dev.probabilities().values(), dtype=np.float64) expected = np.abs(func(theta) @ state) ** 2 diff --git a/tests/test_device.py b/tests/test_device.py index 0bff32c..35fafab 100755 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -16,7 +16,7 @@ import numpy as np -from plugin_name.qiskit_device import z_eigs +from plugin_name.device import z_eigs from plugin_name import Device1