Skip to content

Commit

Permalink
Updating descriptions and replacing 'tns' with 'tn'
Browse files Browse the repository at this point in the history
  • Loading branch information
PietropaoloFrisoni committed Jun 3, 2024
1 parent 049ceee commit 0eb2dfe
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
6 changes: 5 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

<h3>New features since last release</h3>

* The `default.tensor` device is introduced to perform tensor network simulation of a quantum circuit.
* The `default.tensor` device now supports the `tn` method to simulate quantum circuits using exact tensor networks.
[(#5786)](https://github.com/PennyLaneAI/pennylane/pull/5786)

* The `default.tensor` device is introduced to perform tensor network simulation of a quantum circuit using the `mps`
(Matrix Product State) method.
[(#5699)](https://github.com/PennyLaneAI/pennylane/pull/5699)

<h3>Improvements 🛠</h3>
Expand Down
21 changes: 11 additions & 10 deletions pennylane/devices/default_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
)
# The set of supported observables.

_methods = frozenset({"mps", "tns"})
_methods = frozenset({"mps", "tn"})
# The set of supported methods.


Expand Down Expand Up @@ -160,7 +160,7 @@ class DefaultTensor(Device):
wires (int, Iterable[Number, str]): Number of wires present on the device, or iterable that
contains unique labels for the wires as numbers (i.e., ``[-1, 0, 2]``) or strings
(``['aux_wire', 'q1', 'q2']``).
method (str): Supported method. Currently, the supported methods are 'mps' (Matrix Product State) and 'tns' (Exact Tensor Network).
method (str): Supported method. Currently, the supported methods are 'mps' (Matrix Product State) and 'tn' (Exact Tensor Network).
dtype (type): Data type for the tensor representation. Must be one of ``np.complex64`` or ``np.complex128``.
**kwargs: keyword arguments for the device, passed to the ``quimb`` backend.
Expand All @@ -172,8 +172,8 @@ class DefaultTensor(Device):
contract (str): The contraction method for applying gates in the MPS method. It can be either ``auto-mps`` or ``nonlocal``.
``nonlocal`` turns each gate into a Matrix Product Operator (MPO) and applies it directly to the MPS,
while ``auto-mps`` swaps nonlocal qubits in 2-qubit gates to be next to each other before applying the gate,
then swaps them back. Default is ``auto-mps``. TODO: change this
# TODO: add options for TNS
then swaps them back. Default is ``auto-mps``. TODO: update description with new choices
# TODO: add kwargs for TN
**Example:**
Expand Down Expand Up @@ -272,7 +272,7 @@ def __init__(

if not accepted_methods(method):
raise ValueError(
f"Unsupported method: {method}. Supported methods are 'mps' (Matrix Product State) and 'tns' (Exact Tensor Network)."
f"Unsupported method: {method}. Supported methods are 'mps' (Matrix Product State) and 'tn' (Exact Tensor Network)."
)

if dtype not in [np.complex64, np.complex128]:
Expand All @@ -285,7 +285,7 @@ def __init__(
self._method = method
self._dtype = dtype

# options both for MPS and TNS
# options both for MPS and TN
# TODO: add options

# options for MPS
Expand Down Expand Up @@ -340,10 +340,10 @@ def _initialize_quimb_circuit(self, wires: qml.wires.Wires) -> None:
cutoff=self._cutoff,
)

elif self.method == "tns":
elif self.method == "tn":
self._quimb_circuit = qtn.Circuit(
psi0=self._initial_tns(wires),
# TODO: add options for TNS
psi0=self._initial_tn(wires),
# TODO: add options for TN
)

else:
Expand All @@ -367,7 +367,7 @@ def _initial_mps(self, wires: qml.wires.Wires) -> "qtn.MatrixProductState":
tags=[str(l) for l in wires.labels] if wires else None,
)

def _initial_tns(self, wires: qml.wires.Wires) -> "qtn.TensorNetwork":
def _initial_tn(self, wires: qml.wires.Wires) -> "qtn.TensorNetwork":
r"""
Return an initial tensor network state to :math:`\ket{0}`.
Expand Down Expand Up @@ -714,3 +714,4 @@ def execute_and_compute_vjp(
raise NotImplementedError(
"The computation of vector-Jacobian product has yet to be implemented for the default.tensor device."
)

Check notice on line 717 in pennylane/devices/default_tensor.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/devices/default_tensor.py#L717

Trailing newlines (trailing-newlines)
8 changes: 4 additions & 4 deletions tests/devices/default_tensor/test_default_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_wires_runtime_error():
@pytest.mark.parametrize("max_bond_dim", [None, 10])
@pytest.mark.parametrize("cutoff", [1e-16, 1e-12])
@pytest.mark.parametrize("contract", ["auto-mps", "nonlocal"])
@pytest.mark.parametrize("method", ["mps", "tns"])
@pytest.mark.parametrize("method", ["mps", "tn"])
def test_kwargs(method, max_bond_dim, cutoff, contract):
"""Test the class initialization with different arguments and returned properties."""

Expand Down Expand Up @@ -191,7 +191,7 @@ def test_invalid_kwarg():
qml.device("default.tensor", wires=0, fake_arg=None)


@pytest.mark.parametrize("method", ["mps", "tns"])
@pytest.mark.parametrize("method", ["mps", "tn"])
def test_method(method):
"""Test the device method."""
assert qml.device("default.tensor", method=method).method == method
Expand All @@ -216,7 +216,7 @@ def test_ivalid_data_type():
qml.device("default.tensor", wires=0, dtype=float)


@pytest.mark.parametrize("method", ["mps", "tns"])
@pytest.mark.parametrize("method", ["mps", "tn"])
class TestSupportedGatesAndObservables:
"""Test that the DefaultTensor device supports all gates and observables that it claims to support."""

Expand Down Expand Up @@ -320,7 +320,7 @@ def test_execute_and_compute_vjp(self):
dev.execute_and_compute_vjp(circuits=None, cotangents=None)


@pytest.mark.parametrize("method", ["mps", "tns"])
@pytest.mark.parametrize("method", ["mps", "tn"])
class TestJaxSupport:
"""Test the JAX support for the DefaultTensor device."""

Expand Down
8 changes: 4 additions & 4 deletions tests/devices/default_tensor/test_tensor_expval.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
@pytest.fixture(
params=[
(np.complex64, "mps"),
(np.complex64, "tns"),
(np.complex64, "tn"),
(np.complex128, "mps"),
(np.complex128, "tns"),
(np.complex128, "tn"),
]
)
def dev(request):
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_multi_wire_identity_expectation(self, theta, phi, dev):
assert np.allclose(1.0, result, tol)

@pytest.mark.parametrize("wires", [([0, 1]), (["a", 1]), (["b", "a"]), ([-1, 2.5])])
@pytest.mark.parametrize("method", ["mps", "tns"])
@pytest.mark.parametrize("method", ["mps", "tn"])
def test_custom_wires(self, theta, phi, wires, method):
"""Tests custom wires."""
dev = qml.device("default.tensor", wires=wires, method=method)
Expand Down Expand Up @@ -361,7 +361,7 @@ def test_PauliZ_hadamard_PauliY(self, theta, phi, varphi, dev):


@pytest.mark.parametrize("theta, phi", list(zip(THETA, PHI)))
@pytest.mark.parametrize("method", ["mps", "tns"])
@pytest.mark.parametrize("method", ["mps", "tn"])
def test_multi_qubit_gates(theta, phi, method):
"""Tests a simple circuit with multi-qubit gates."""

Expand Down
8 changes: 4 additions & 4 deletions tests/devices/default_tensor/test_tensor_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
@pytest.fixture(
params=[
(np.complex64, "mps"),
(np.complex64, "tns"),
(np.complex64, "tn"),
(np.complex128, "mps"),
(np.complex128, "tns"),
(np.complex128, "tn"),
]
)
def dev(request):
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_multi_wire_identity_variance(self, theta, phi, dev):
assert np.allclose(0.0, result, atol=tol, rtol=0)

@pytest.mark.parametrize("wires", [([0, 1]), (["a", 1]), (["b", "a"]), ([-1, 2.5])])
@pytest.mark.parametrize("method", ["mps", "tns"])
@pytest.mark.parametrize("method", ["mps", "tn"])
def test_custom_wires(self, theta, phi, wires, method):
"""Tests custom wires."""
device = qml.device("default.tensor", wires=wires, method=method)
Expand Down Expand Up @@ -371,7 +371,7 @@ def test_PauliZ_hadamard_PauliY(self, theta, phi, varphi, dev):
# in the tape computation with `default.qubit`, that we use as reference.
@pytest.mark.usefixtures("new_opmath_only")
@pytest.mark.parametrize("theta, phi", list(zip(THETA, PHI)))
@pytest.mark.parametrize("method", ["mps", "tns"])
@pytest.mark.parametrize("method", ["mps", "tn"])
def test_multi_qubit_gates(theta, phi, method):
"""Tests a simple circuit with multi-qubit gates."""

Expand Down

0 comments on commit 0eb2dfe

Please sign in to comment.