Skip to content

Commit

Permalink
style fix
Browse files Browse the repository at this point in the history
  • Loading branch information
josephleekl committed Jan 22, 2025
1 parent 4835b1f commit f34bf83
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
10 changes: 5 additions & 5 deletions pennylane_lightning/lightning_qubit/_state_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def state(self):
state = np.zeros(2**self._num_wires, dtype=self.dtype)
self._qubit_state.getState(state)
return state

Check notice on line 79 in pennylane_lightning/lightning_qubit/_state_vector.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane_lightning/lightning_qubit/_state_vector.py#L79

Trailing whitespace (trailing-whitespace)
def update_num_qubits(self, new_num_wires: int):

Check notice on line 80 in pennylane_lightning/lightning_qubit/_state_vector.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane_lightning/lightning_qubit/_state_vector.py#L80

Missing function or method docstring (missing-function-docstring)
self._num_wires = new_num_wires
self._qubit_state.updateNumQubits(new_num_wires)
self.reset_state()

Check warning on line 83 in pennylane_lightning/lightning_qubit/_state_vector.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_qubit/_state_vector.py#L81-L83

Added lines #L81 - L83 were not covered by tests

def _state_dtype(self):
"""Binding to Lightning Managed state vector C++ class.
Expand All @@ -84,11 +89,6 @@ def _state_dtype(self):
"""
return StateVectorC128 if self.dtype == np.complex128 else StateVectorC64

def _update_num_qubits(self, new_num_wires: int):
self._num_wires = new_num_wires
self._qubit_state.updateNumQubits(new_num_wires)
self.reset_state()

def _apply_state_vector(self, state, device_wires: Wires):
"""Initialize the internal state vector in a specified state.
Args:
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/lightning_qubit/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def execute(
circuit = circuit.map_to_standard_wires()

Check warning on line 404 in pennylane_lightning/lightning_qubit/lightning_qubit.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_qubit/lightning_qubit.py#L404

Added line #L404 was not covered by tests
else:
if not self.wires:
self._statevector._update_num_qubits(circuit.num_wires)
self._statevector.update_num_qubits(circuit.num_wires)
circuit = circuit.map_to_standard_wires()

Check warning on line 408 in pennylane_lightning/lightning_qubit/lightning_qubit.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_qubit/lightning_qubit.py#L406-L408

Added lines #L406 - L408 were not covered by tests

if self._wire_map is not None:
Expand Down
24 changes: 3 additions & 21 deletions tests/lightning_qubit/test_state_vector_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,39 +485,21 @@ def test_get_final_state(tol, operation, input, expected_output, par):
assert final_state == state_vector


def test_dynamically_allocate_qubit(tol, operation, input, expected_output, par):
"""Tests that applying an operation yields the expected output state for two wire
operations that have parameters."""
if device_name != "lightning.qubit":
pytest.skip("Only Lightning Qubit allows dynamic qubit allocation", allow_module_level=True)

wires = 2
state_vector = LightningStateVector(wires)
tape = QuantumScript(
[qml.StatePrep(np.array(input), Wires(range(wires))), operation(*par, Wires(range(wires)))]
)
final_state = state_vector.get_final_state(tape)

assert np.allclose(final_state.state, np.array(expected_output), atol=tol, rtol=0)
assert final_state.state.dtype == final_state.dtype
assert final_state == state_vector


@pytest.mark.parametrize("num_wires", range(2, 5))
@pytest.mark.parametrize("dtype", [np.complex64, np.complex128])
def test_update_num_qubit(num_wires, dtype):
""" """
"""Tests that the state vector is correctly updated when the number of qubits is changed."""
if device_name != "lightning.qubit":
pytest.skip("Only Lightning Qubit allows dynamic qubit allocation")

state_vector = LightningStateVector(num_wires, dtype=dtype)

state_vector._update_num_qubits(num_wires + 2)
state_vector.update_num_qubits(num_wires + 2)
expected_output = np.zeros(2 ** (num_wires + 2), dtype=dtype)
expected_output[0] = 1
assert np.allclose(state_vector.state, expected_output)

state_vector._update_num_qubits(num_wires - 1)
state_vector.update_num_qubits(num_wires - 1)
expected_output = np.zeros(2 ** (num_wires - 1), dtype=dtype)
expected_output[0] = 1
assert np.allclose(state_vector.state, expected_output)

0 comments on commit f34bf83

Please sign in to comment.