Skip to content

Commit

Permalink
Correcting tests (#49)
Browse files Browse the repository at this point in the history
* Changing to picking a valid four qubit lattice at runtime instead of hardcoding it; updating the number of shots; updating the name of the Variable class attribute for argument values

* Rename probability to analytic_probability; moving it into the NumpyWavefunctionDevice class (simulator only)

* Moving the analytic_probability function back into ForestDevice

* Removing whitespace

* Temporarily moving to the latest version of PL

* Modifying the number of shots
  • Loading branch information
antalszava committed Apr 10, 2020
1 parent 6f0edf6 commit 2ea5232
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pennylane_forest/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def mat_vec_product(self, mat, vec, wires):

return np.reshape(state_multi_index, 2 ** self.num_wires)

def probability(self, wires=None):
def analytic_probability(self, wires=None):
"""Return the (marginal) probability of each computational basis
state from the last run of the device.
Expand Down
2 changes: 1 addition & 1 deletion pennylane_forest/qpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(
self,
device,
*,
shots=1024,
shots=1000,
active_reset=True,
load_qc=True,
readout_error=None,
Expand Down
2 changes: 1 addition & 1 deletion pennylane_forest/qvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class QVMDevice(ForestDevice):
short_name = "forest.qvm"
observables = {"PauliX", "PauliY", "PauliZ", "Identity", "Hadamard", "Hermitian"}

def __init__(self, device, *, shots=1024, noisy=False, **kwargs):
def __init__(self, device, *, shots=1000, noisy=False, **kwargs):

if shots <= 0:
raise ValueError("Number of shots must be a positive integer.")
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
pennylane>=0.8
git+https://github.com/XanaduAI/pennylane.git#egg=pennylane
networkx
flaky
19 changes: 13 additions & 6 deletions tests/test_qpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
qc for qc in pyquil.list_quantum_computers() if "qvm" not in qc and re.match(pattern, qc)
]

pattern_four_qubit = "Aspen-.-4Q-."
VALID_FOUR_QUBIT_LATTICES = [
qc for qc in pyquil.list_quantum_computers() if "qvm" not in qc and re.match(pattern_four_qubit, qc)
]


class TestQPUIntegration(BaseTest):
"""Test the wavefunction simulator works correctly from the PennyLane frontend."""
Expand All @@ -33,7 +38,7 @@ def test_load_qpu_device(self):
device = [qpu for qpu in VALID_QPU_LATTICES if "-2Q" in qpu][0]
dev = qml.device("forest.qpu", device=device, load_qc=False)
self.assertEqual(dev.num_wires, 2)
self.assertEqual(dev.shots, 1024)
self.assertEqual(dev.shots, 1000)
self.assertEqual(dev.short_name, "forest.qpu")

def test_load_virtual_qpu_device(self):
Expand Down Expand Up @@ -67,17 +72,18 @@ def test_tensor_wires_expval_parametric_compilation(self, obs):
As the results coming from the qvm are stochastic, a constraint of 3 out of 5 runs was added.
"""
device = np.random.choice(VALID_FOUR_QUBIT_LATTICES)
p = np.pi / 8
dev = qml.device(
"forest.qpu",
device="Aspen-4-4Q-E",
device=device,
shots=10000,
load_qc=False,
parametric_compilation=True,
)
dev_1 = qml.device(
"forest.qpu",
device="Aspen-4-4Q-E",
device=device,
shots=10000,
load_qc=False,
parametric_compilation=True,
Expand Down Expand Up @@ -113,17 +119,18 @@ def test_tensor_wires_expval_operator_estimation(self, obs):
As the results coming from the qvm are stochastic, a constraint of 3 out of 5 runs was added.
"""
device = np.random.choice(VALID_FOUR_QUBIT_LATTICES)
p = np.pi / 7
dev = qml.device(
"forest.qpu",
device="Aspen-4-4Q-E",
device=device,
shots=1000,
load_qc=False,
parametric_compilation=False,
)
dev_1 = qml.device(
"forest.qpu",
device="Aspen-4-4Q-E",
device=device,
shots=1000,
load_qc=False,
parametric_compilation=False,
Expand Down Expand Up @@ -161,7 +168,7 @@ def test_warnings_raised_parametric_compilation_and_operator_estimation(self):
with pytest.warns(Warning, match="Operator estimation is being turned off."):
dev = qml.device(
"forest.qpu",
device="Aspen-4-4Q-E",
device=device,
shots=1000,
load_qc=False,
parametric_compilation=True,
Expand Down
11 changes: 6 additions & 5 deletions tests/test_qvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,8 @@ def test_parametric_compilation_with_numeric_and_symbolic_queue(
):
"""Tests that a program containing numeric and symbolic variables as well is only compiled once."""

Variable.free_param_values = {}
Variable.positional_arg_values = {}

dev = qml.device("forest.qvm", device="2q-qvm", timeout=100)

dev._circuit_hash = None
Expand All @@ -634,8 +635,8 @@ def test_parametric_compilation_with_numeric_and_symbolic_queue(

call_history = []
for run_idx in range(number_of_runs):
Variable.free_param_values[1] = 0.232 * run_idx
Variable.free_param_values[2] = 0.8764 * run_idx
Variable.positional_arg_values[1] = 0.232 * run_idx
Variable.positional_arg_values[2] = 0.8764 * run_idx
circuit_graph = CircuitGraph(queue, observable_queue)

dev.apply(circuit_graph.operations, rotations=circuit_graph.diagonalizing_gates)
Expand Down Expand Up @@ -695,15 +696,15 @@ def test_load_qvm_device(self, qvm):
"""Test that the QVM device loads correctly"""
dev = qml.device("forest.qvm", device="2q-qvm")
self.assertEqual(dev.num_wires, 2)
self.assertEqual(dev.shots, 1024)
self.assertEqual(dev.shots, 1000)
self.assertEqual(dev.short_name, "forest.qvm")

def test_load_qvm_device_from_topology(self, qvm):
"""Test that the QVM device, from an input topology, loads correctly"""
topology = nx.complete_graph(2)
dev = qml.device("forest.qvm", device=topology)
self.assertEqual(dev.num_wires, 2)
self.assertEqual(dev.shots, 1024)
self.assertEqual(dev.shots, 1000)
self.assertEqual(dev.short_name, "forest.qvm")

def test_load_virtual_qpu_device(self, qvm):
Expand Down

0 comments on commit 2ea5232

Please sign in to comment.