Skip to content

Commit

Permalink
Define state method and returns_state=True in capabilities (#101)
Browse files Browse the repository at this point in the history
* matrix update

* matrix update

* def state

* def state + capabilities

* test dev.state

* changelog

* linting

* write dev._state instead of dev.state
  • Loading branch information
antalszava committed Jun 8, 2022
1 parent ee8b9a0 commit ddfd8e5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Release 0.21.0-dev
# Release 0.24.0-dev

### New features since last release

Expand All @@ -10,10 +10,17 @@

### Bug fixes

* Defines the missing `state` method and `returns_state` entry of the
`capabilities` dictionary for `forest.wavefunction` and
`forest.numpy_wavefunction`.
[(#36)](https://github.com/PennyLaneAI/pennylane-qulacs/pull/36)

### Contributors

This release contains contributions from (in alphabetical order):

Antal Száva.

---

# Release 0.20.0
Expand Down
16 changes: 12 additions & 4 deletions pennylane_forest/numpy_wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
Code details
~~~~~~~~~~~~
"""
import itertools

import numpy as np

from pyquil.pyqvm import PyQVM
from pyquil.simulation import NumpyWavefunctionSimulator

Expand Down Expand Up @@ -50,6 +46,18 @@ def __init__(self, wires, *, shots=None, **kwargs):
self.qc = PyQVM(n_qubits=len(self.wires), quantum_simulator_type=NumpyWavefunctionSimulator)
self._state = None

@classmethod
def capabilities(cls): # pylint: disable=missing-function-docstring
capabilities = super().capabilities().copy()
capabilities.update(
returns_state=True,
)
return capabilities

@property
def state(self): # pylint: disable=missing-function-docstring
return self._state

def apply(self, operations, **kwargs):
self.reset()
self.qc.wf_simulator.reset()
Expand Down
16 changes: 12 additions & 4 deletions pennylane_forest/wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
Code details
~~~~~~~~~~~~
"""
import itertools

import numpy as np
from numpy.linalg import eigh
from pennylane.wires import Wires

from pyquil.api import WavefunctionSimulator

Expand Down Expand Up @@ -105,6 +101,18 @@ def bit2dec(x):
y += j << i
return y

@classmethod
def capabilities(cls): # pylint: disable=missing-function-docstring
capabilities = super().capabilities().copy()
capabilities.update(
returns_state=True,
)
return capabilities

@property
def state(self): # pylint: disable=missing-function-docstring
return self._state

def expand_state(self):
"""The pyQuil wavefunction simulator initializes qubits dymnically as they are requested.
This method expands the state to the full number of wires in the device."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_numpy_wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_apply(self, op, apply_unitary, tol):
dev.apply(tape.operations, rotations=tape.diagonalizing_gates)

# verify the device is now in the expected state
self.assertAllAlmostEqual(dev._state, state, delta=tol)
self.assertAllAlmostEqual(dev.state, state, delta=tol)

def test_sample_values(self, tol):
"""Tests if the samples returned by sample have
Expand Down
6 changes: 3 additions & 3 deletions tests/test_wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_apply(self, op, apply_unitary, tol):
dev.apply(tape.operations, rotations=tape.diagonalizing_gates)

# verify the device is now in the expected state
self.assertAllAlmostEqual(dev._state, state, delta=tol)
self.assertAllAlmostEqual(dev.state, state, delta=tol)

def test_sample_values(self, tol):
"""Tests if the samples returned by sample have
Expand Down Expand Up @@ -390,13 +390,13 @@ def test_expand_state(self):
dev._state = np.array([0, 1, 1, 0]) / np.sqrt(2)
dev._active_wires = Wires([0, 2])
dev.expand_state()
self.assertAllEqual(dev._state, np.array([0, 1, 0, 0, 1, 0, 0, 0]) / np.sqrt(2))
self.assertAllEqual(dev.state, np.array([0, 1, 0, 0, 1, 0, 0, 0]) / np.sqrt(2))

# expand a three qubit state to the 3 qubit device
dev._state = np.array([0, 1, 1, 0, 0, 1, 1, 0]) / 2
dev._active_wires = Wires([0, 1, 2])
dev.expand_state()
self.assertAllEqual(dev._state, np.array([0, 1, 1, 0, 0, 1, 1, 0]) / 2)
self.assertAllEqual(dev.state, np.array([0, 1, 1, 0, 0, 1, 1, 0]) / 2)


@pytest.mark.parametrize("bitstring, dec", [([1], 1), ([0,0,1], 1), ([0,1,0], 2), ([1,1,1], 7)])
Expand Down

0 comments on commit ddfd8e5

Please sign in to comment.