Skip to content

Commit

Permalink
Remove qubit unitary check and adjust wavefunction simulator tests (#81)
Browse files Browse the repository at this point in the history
* adjust tests, changelog

* pin to rc
  • Loading branch information
antalszava committed Aug 11, 2021
1 parent e931afb commit 55ec310
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 37 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release 0.17.0

### Improvements

* Removed a validation check for ``QubitUnitary`` that is now in PennyLane
core.
[(#74)](https://github.com/PennyLaneAI/pennylane-forest/pull/74)

### Bug fixes

* Pins the PyQuil version to use as `pyquil>=2.16,<2.28.3` due to API
Expand All @@ -10,7 +16,7 @@

This release contains contributions from (in alphabetical order):

Romain Moyard, Antal Száva.
Theodor Isacsson, Romain Moyard, Antal Száva.

---

Expand Down
9 changes: 0 additions & 9 deletions pennylane_forest/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ def qubit_unitary(par, *wires):
Returns:
list: list of PauliX matrix operators acting on each wire
"""
if par.shape[0] != par.shape[1]:
raise ValueError("Qubit unitary must be a square matrix.")

if not np.allclose(par @ par.conj().T, np.identity(par.shape[0])):
raise ValueError("Qubit unitary matrix must be unitary.")

if par.shape != tuple([2 ** len(wires)] * 2):
raise ValueError("Qubit unitary matrix must be 2^Nx2^N, where N is the number of wires.")

# Get the Quil definition for the new gate
gate_definition = DefGate("U_{}".format(str(uuid.uuid4())[:8]), par)
# Get the gate constructor
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pyquil>=2.16,<2.28.3
pennylane>=0.15
git+https://github.com/PennyLaneAI/pennylane.git@v0.17.0-rc0
networkx
flaky
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
version = f.readlines()[-1].split()[-1].strip("\"'")


requirements = ["pyquil>=2.16,<2.28.3", "pennylane>=0.15"]
requirements = ["pyquil>=2.16,<2.28.3", "pennylane @ git+https://github.com/PennyLaneAI/pennylane.git@v0.17.0-rc0"]

info = {
"name": "PennyLane-Forest",
Expand Down
17 changes: 4 additions & 13 deletions tests/test_numpy_wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_sample_values(self, tol):

dev.apply(tape._ops, rotations=tape.diagonalizing_gates)
dev._samples = dev.generate_samples()
s1 = dev.sample(O)
s1 = dev.sample(O.obs)

# s1 should only contain 1 and -1
self.assertAllAlmostEqual(s1 ** 2, 1, delta=tol)
Expand All @@ -176,7 +176,7 @@ def test_sample_values_hermitian(self, tol):

dev._samples = dev.generate_samples()

s1 = dev.sample(O)
s1 = dev.sample(O.obs)

# s1 should only contain the eigenvalues of
# the hermitian matrix
Expand Down Expand Up @@ -221,7 +221,7 @@ def test_sample_values_hermitian_multi_qubit(self, tol):

dev._samples = dev.generate_samples()

s1 = dev.sample(O)
s1 = dev.sample(O.obs)

# s1 should only contain the eigenvalues of
# the hermitian matrix
Expand Down Expand Up @@ -334,17 +334,9 @@ def circuit(Umat):
return qml.expval(qml.PauliZ(0))

circuit1 = qml.QNode(circuit, dev)
with pytest.raises(ValueError, match="must be a square matrix"):
with pytest.raises(ValueError, match="Input unitary must be of shape"):
circuit1(np.array([[0, 1]]))

circuit1 = qml.QNode(circuit, dev)
with pytest.raises(ValueError, match="must be unitary"):
circuit1(np.array([[1, 1], [1, 1]]))

circuit1 = qml.QNode(circuit, dev)
with pytest.raises(ValueError, match=r"must be 2\^Nx2\^N"):
circuit1(U)

def test_one_qubit_wavefunction_circuit(self, tol):
"""Test that the wavefunction plugin provides correct result for simple circuit"""
dev = qml.device("forest.numpy_wavefunction", wires=1)
Expand All @@ -361,7 +353,6 @@ def circuit(x, y, z):
qml.Rot(x, y, z, wires=0)
return qml.expval(qml.PauliZ(0))

print(circuit(a, b, c))
self.assertAlmostEqual(circuit(a, b, c), np.cos(a) * np.sin(b), delta=tol)

def test_two_qubit_wavefunction_circuit(self, tol):
Expand Down
16 changes: 4 additions & 12 deletions tests/test_wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_sample_values(self, tol):

dev.apply(tape._ops, rotations=tape.diagonalizing_gates)
dev._samples = dev.generate_samples()
s1 = dev.sample(O)
s1 = dev.sample(O.obs)

# s1 should only contain 1 and -1
self.assertAllAlmostEqual(s1 ** 2, 1, delta=tol)
Expand All @@ -175,7 +175,7 @@ def test_sample_values_hermitian(self, tol):

dev._samples = dev.generate_samples()

s1 = dev.sample(O)
s1 = dev.sample(O.obs)

# s1 should only contain the eigenvalues of
# the hermitian matrix
Expand Down Expand Up @@ -220,7 +220,7 @@ def test_sample_values_hermitian_multi_qubit(self, tol):

dev._samples = dev.generate_samples()

s1 = dev.sample(O)
s1 = dev.sample(O.obs)

# s1 should only contain the eigenvalues of
# the hermitian matrix
Expand Down Expand Up @@ -318,17 +318,9 @@ def circuit(Umat):
return qml.expval(qml.PauliZ(0))

circuit1 = qml.QNode(circuit, dev)
with pytest.raises(ValueError, match="must be a square matrix"):
with pytest.raises(ValueError, match="Input unitary must be of shape"):
circuit1(np.array([[0, 1]]))

circuit1 = qml.QNode(circuit, dev)
with pytest.raises(ValueError, match="must be unitary"):
circuit1(np.array([[1, 1], [1, 1]]))

circuit1 = qml.QNode(circuit, dev)
with pytest.raises(ValueError, match=r"must be 2\^Nx2\^N"):
circuit1(U)

def test_one_qubit_wavefunction_circuit(self, tol, qvm, compiler):
"""Test that the wavefunction plugin provides correct result for simple circuit"""
dev = qml.device("forest.wavefunction", wires=1)
Expand Down

0 comments on commit 55ec310

Please sign in to comment.