Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/readout snowflurry0.5.1 #35

Draft
wants to merge 11 commits into
base: dev
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ docs/_build/*
build
__pycache__/
*.whl
.coverage*
.xml
2 changes: 1 addition & 1 deletion pennylane_snowflurry/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"""


__version__ = "0.4.2"
__version__ = "0.4.3"
2 changes: 1 addition & 1 deletion pennylane_snowflurry/julia_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Required packages for the Julia environment
REQUIRED_PACKAGES = [
PkgSpec(
name="Snowflurry", uuid="7bd9edc1-4fdc-40a1-a0f6-da58fb4f45ec", version="0.4"
name="Snowflurry", uuid="7bd9edc1-4fdc-40a1-a0f6-da58fb4f45ec", version="0.5"
),
]

Expand Down
4 changes: 0 additions & 4 deletions pennylane_snowflurry/pennylane_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ class PennylaneConverter:
def __init__(
self,
pennylane_circuit: QuantumTape,
debugger=None,
interface=None,
host="",
user="",
access_token="",
Expand All @@ -108,8 +106,6 @@ def __init__(

# Instance attributes related to PennyLane
self.pennylane_circuit = pennylane_circuit
self.debugger = debugger
self.interface = interface
self.wires = wires

# Instance attributes related to Snowflurry
Expand Down
2 changes: 0 additions & 2 deletions pennylane_snowflurry/snowflurry_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ def execute(
results = tuple(
PennylaneConverter(
circuit,
debugger=self._debugger,
interface=interface,
host=self.host,
user=self.user,
access_token=self.access_token,
Expand Down
97 changes: 86 additions & 11 deletions tests/test_pennylaneConverter.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,92 @@
import pennylane_snowflurry
import pytest
from pennylane_snowflurry import measurements
from pennylane_snowflurry import PennylaneConverter
import pennylane as qml
import numpy as np
import logging
from pennylane.tape import QuantumTape
import unittest

class Test_PennylaneConverterClass(unittest.TestCase):
def test_quantumTape(self):
class TestPennylaneConverterClass:

@pytest.fixture
def client_creds(self):
return {
"host": "host_url",
"user": "user",
"access_token": "access_token",
"project_id": "project_id",
"realm": "realm"
}

@pytest.fixture
def bell_state_operations(self):
ops = [qml.Hadamard(wires=0), qml.CNOT(wires=[0, 1])]
return ops

@pytest.fixture
def bell_state_quantum_tape(self, bell_state_operations):
ops = bell_state_operations
return QuantumTape(ops, [qml.expval(qml.PauliZ(0))], shots=100)

@pytest.fixture
def pennylane_converter_with_client(self, bell_state_quantum_tape, client_creds):
_pennylane_circuit = bell_state_quantum_tape
_host = client_creds["host"]
_user = client_creds["user"]
_access_token = client_creds["access_token"]
_project_id = client_creds["project_id"]
_realm = client_creds["realm"]
_wires = [0, 1]
return PennylaneConverter(pennylane_circuit=_pennylane_circuit, host=_host, user=_user, access_token=_access_token, project_id=_project_id, realm=_realm, wires=_wires)

@pytest.fixture
def pennylane_converter_without_client(self, bell_state_quantum_tape):
_pennylane_circuit = bell_state_quantum_tape
_wires = [0, 1]
return PennylaneConverter(pennylane_circuit=_pennylane_circuit, wires=_wires)

@pytest.fixture
def mock_get_strategy(self):
return measurements.Sample()

def test_quantum_tape(self):
ops = [qml.BasisState(np.array([1,1]), wires=(0,"a"))]
quantumTape = QuantumTape(ops, [qml.expval(qml.PauliZ(0))])
converter = pennylane_snowflurry.PennylaneConverter(quantumTape)
self.assertIsInstance(converter.pennylane_circuit, QuantumTape)

quantum_tape = QuantumTape(ops, [qml.expval(qml.PauliZ(0))])
converter = PennylaneConverter(quantum_tape)
assert isinstance(converter.pennylane_circuit, QuantumTape)

def test_initialization_without_client(self, bell_state_quantum_tape, pennylane_converter_without_client):
assert pennylane_converter_without_client.pennylane_circuit == bell_state_quantum_tape
assert pennylane_converter_without_client.wires == [0, 1]

def test_get_strategy(self, pennylane_converter_without_client):
# CountsMP
mp = qml.measurements.CountsMP()
strategy = pennylane_converter_without_client.get_strategy(mp)
assert isinstance(strategy, measurements.Counts)

# SampleMP
mp = qml.measurements.SampleMP()
strategy = pennylane_converter_without_client.get_strategy(mp)
assert isinstance(strategy, measurements.Sample)

# ProbabilityMP
mp = qml.measurements.ProbabilityMP()
strategy = pennylane_converter_without_client.get_strategy(mp)
assert isinstance(strategy, measurements.Probabilities)

# ExpectationMP
mp = qml.measurements.ExpectationMP()
strategy = pennylane_converter_without_client.get_strategy(mp)
assert isinstance(strategy, measurements.ExpectationValue)

# StateMP
mp = qml.measurements.StateMP()
strategy = pennylane_converter_without_client.get_strategy(mp)
assert isinstance(strategy, measurements.State)







if __name__ == '__main__':
unittest.main()
38 changes: 14 additions & 24 deletions tests/test_pennylaneSubroutine.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
import pennylane as qml
from pennylane import numpy as np
import unittest
import matplotlib.pyplot as plt
import numpy as np
import pytest


# This test class is meant to verify compatibility of PennyLane subroutines with the plugin.
# A listing of the subroutines can be found at the following links:
# https://docs.pennylane.ai/en/stable/introduction/operations.html
# https://docs.pennylane.ai/en/stable/code/qml.html#classes


class TestPennylaneSubroutine(unittest.TestCase):
class TestPennylaneSubroutine:
"""Test the PennyLane subroutine functions with 'snowflurry.qubit'.
The decompose transforms implemented in the plugin should return the same
state as the PennyLane 'default.qubit'.
"""

# def test_circuit(subroutine):
# subroutine
# return qml.state()

def setUp(self):
"""
Set up the conditions for the test.
"""
@pytest.fixture(autouse=True)
def dev_pennylane(self):
self.dev_pennylane = qml.device("default.qubit", wires=5)

@pytest.fixture(autouse=True)
def dev_snowflurry(self):
self.dev_snowflurry = qml.device("snowflurry.qubit", wires=5)

def test_AQFT(self):
"""Test the AQFT subroutine."""
print("Testing AQFT subroutine")

def circuit_AQFT():
qml.AQFT(order=1, wires=[0, 1, 2, 3, 4])
return qml.state()

pennylane_qnode = qml.QNode(circuit_AQFT, self.dev_pennylane)
snowflurry_qnode = qml.QNode(circuit_AQFT, self.dev_snowflurry)
result_pennylane = pennylane_qnode()
pennylane_qnode = qml.QNode(circuit_AQFT, self.dev_pennylane)
result_snowflurry = snowflurry_qnode()
self.assertTrue(np.allclose(result_pennylane, result_snowflurry))
result_pennylane = pennylane_qnode()
assert result_pennylane.all() == result_snowflurry.all()


def test_BasisState(self):
"""Test the BasisState subroutine.
Expand All @@ -58,7 +52,7 @@ def circuit_BasisState_all_ones():
snowflurry_qnode = qml.QNode(circuit_BasisState_all_ones, self.dev_snowflurry)
result_pennylane = pennylane_qnode()
result_snowflurry = snowflurry_qnode()
self.assertTrue(np.allclose(result_pennylane, result_snowflurry))
assert result_pennylane.all() == result_snowflurry.all()

def circuit_BasisState_all_zeros():
qml.BasisState(np.array([0, 0, 0, 0, 0]), wires=[0, 1, 2, 3, 4])
Expand All @@ -68,8 +62,4 @@ def circuit_BasisState_all_zeros():
snowflurry_qnode = qml.QNode(circuit_BasisState_all_zeros, self.dev_snowflurry)
result_pennylane = pennylane_qnode()
result_snowflurry = snowflurry_qnode()
self.assertTrue(np.allclose(result_pennylane, result_snowflurry))


if __name__ == "__main__":
unittest.main()
assert result_pennylane.all() == result_snowflurry.all()
Loading