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

Add test_templates module + Fix LQ decomposition of StatePrep Ops + Fix LQ decomposition strategy of QFT and GroverOperator #684

Merged
merged 25 commits into from
Apr 18, 2024
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
41d77fd
Add test for QSVT.
vincentmr Apr 16, 2024
e890a52
Auto update version
github-actions[bot] Apr 16, 2024
243b121
Update changelog.
vincentmr Apr 16, 2024
3a59e7e
Add tests for embedding, layer and stateprep templates.
vincentmr Apr 16, 2024
8667022
Parametrize tests over n_qubits.
vincentmr Apr 16, 2024
ca467db
Add qchem template tests.
vincentmr Apr 16, 2024
92744f3
Add a few misc template tests.
vincentmr Apr 16, 2024
31f73ea
Merge branch 'master' into feature/test_templates
vincentmr Apr 17, 2024
a776b81
Auto update version
github-actions[bot] Apr 17, 2024
369739e
trigger ci
vincentmr Apr 17, 2024
a4985de
Fix serialize.
vincentmr Apr 17, 2024
103bad1
Fix formatting.
vincentmr Apr 17, 2024
fc89d8d
Update tests/test_templates.py
vincentmr Apr 17, 2024
ecb49e0
Update tests/test_templates.py
vincentmr Apr 17, 2024
7aaef8a
Add xfail condition for AmplitudeEmbedding.
vincentmr Apr 17, 2024
1412d42
Fix pytest.skip top cond.
vincentmr Apr 17, 2024
1f9ed97
Update tests/test_templates.py
vincentmr Apr 17, 2024
3c5395c
Update pennylane_lightning/core/_serialize.py
vincentmr Apr 17, 2024
cea50c1
Add breaking tests for if not observable
vincentmr Apr 17, 2024
1a65c8f
trigger ci
vincentmr Apr 17, 2024
8dc58ab
Merge branch 'master' into feature/test_templates
vincentmr Apr 17, 2024
201d14a
Auto update version
github-actions[bot] Apr 17, 2024
50b7fa9
Fix decompositions with `LightningQubit` (#687)
mudit2812 Apr 18, 2024
b10c6fb
Merge branch 'master' into feature/test_templates
maliasadi Apr 18, 2024
a0c6265
Fix test params
vincentmr Apr 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Parametrize tests over n_qubits.
  • Loading branch information
vincentmr committed Apr 16, 2024
commit 8667022c516050313cb92e89ba59b693264ea907
120 changes: 64 additions & 56 deletions tests/test_templates.py
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
class TestGrover:
"""Test Grover's algorithm (multi-controlled gates, decomposition, etc.)"""

@pytest.mark.parametrize("num_qubits", range(4, 8))
def test_grover(self, num_qubits):
@pytest.mark.parametrize("n_qubits", range(4, 8))
def test_grover(self, n_qubits):
np.random.seed(42)
omega = np.random.rand(num_qubits) > 0.5
dev = qml.device(device_name, wires=num_qubits)
wires = list(range(num_qubits))
omega = np.random.rand(n_qubits) > 0.5
dev = qml.device(device_name, wires=n_qubits)
wires = list(range(n_qubits))

@qml.qnode(dev, diff_method=None)
def circuit(omega):
iterations = int(np.round(np.sqrt(2**num_qubits) * np.pi / 4))
iterations = int(np.round(np.sqrt(2**n_qubits) * np.pi / 4))

# Initial state preparation
for wire in wires:
Expand Down Expand Up @@ -65,8 +65,9 @@ def circuit(omega):
class TestQSVT:
"""Test the QSVT algorithm."""

def test_qsvt(self):
dev = qml.device(device_name, wires=2)
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_qsvt(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")
A = np.array([[0.1]])
block_encode = qml.BlockEncode(A, wires=[0, 1])
Expand All @@ -85,8 +86,8 @@ def circuit():
class TestAngleEmbedding:
"""Test the AngleEmbedding algorithm."""

def test_angleembedding(self):
n_qubits = 3
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_angleembedding(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

Expand All @@ -95,7 +96,7 @@ def circuit(feature_vector):
qml.Hadamard(0)
return qml.probs(wires=range(n_qubits))

X = [1, 2, 3]
X = np.random.rand(n_qubits)

res = qml.QNode(circuit, dev, diff_method=None)(X)
ref = qml.QNode(circuit, dq, diff_method=None)(X)
Expand All @@ -106,18 +107,26 @@ def circuit(feature_vector):
class TestAmplitudeEmbedding:
"""Test the AmplitudeEmbedding algorithm."""

def test_amplitudeembedding(self):
n_qubits = 2
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_amplitudeembedding(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(f=None):
qml.AmplitudeEmbedding(features=f, wires=range(n_qubits))
return qml.expval(qml.Z(0)), qml.state()

X = [1 / 2, 1 / 2, 1 / 2, 1 / 2]
res, res_state = qml.QNode(circuit, dev, diff_method=None)(f=X)
ref, ref_state = qml.QNode(circuit, dq, diff_method=None)(f=X)
if LightningDevice._new_API:
return qml.expval(qml.Z(0)), qml.state()
else:
return qml.state()

X = np.random.rand(2**n_qubits)
X /= np.linalg.norm(X)
if LightningDevice._new_API:
res, res_state = qml.QNode(circuit, dev, diff_method=None)(f=X)
ref, ref_state = qml.QNode(circuit, dq, diff_method=None)(f=X)
else:
res, res_state = 1.0, qml.QNode(circuit, dev, diff_method=None)(f=X)
ref, ref_state = 1.0, qml.QNode(circuit, dq, diff_method=None)(f=X)

assert np.allclose(res, ref)
assert np.allclose(res_state, ref_state)
Expand All @@ -126,16 +135,16 @@ def circuit(f=None):
class TestBasisEmbedding:
"""Test the BasisEmbedding algorithm."""

def test_basisembedding(self):
n_qubits = 3
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_basisembedding(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(feature_vector):
qml.BasisEmbedding(features=feature_vector, wires=range(n_qubits))
return qml.state()

X = [1, 1, 1]
X = np.ones(n_qubits)

res = qml.QNode(circuit, dev, diff_method=None)(X)
ref = qml.QNode(circuit, dq, diff_method=None)(X)
Expand All @@ -146,35 +155,35 @@ def circuit(feature_vector):
class TestDisplacementEmbedding:
"""Test the DisplacementEmbedding algorithm."""

@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
@pytest.mark.parametrize("template", [qml.DisplacementEmbedding, qml.SqueezingEmbedding])
def test_displacementembedding(self, template):
n_qubits = 3
def test_displacementembedding(self, n_qubits, template):
dev = qml.device(device_name, wires=n_qubits)

def circuit(feature_vector):
template(features=feature_vector, wires=range(n_qubits))
qml.QuadraticPhase(0.1, wires=1)
return qml.expval(qml.NumberOperator(wires=1))

X = [1, 2, 3]
X = np.arange(1, n_qubits + 1)

with pytest.raises(qml._device.DeviceError, match=f"not supported on {device_name}"):
with pytest.raises(qml._device.DeviceError, match="not supported"):
_ = qml.QNode(circuit, dev, diff_method=None)(X)


class TestIQPEmbedding:
"""Test the IQPEmbedding algorithm."""

def test_iqpembedding(self):
n_qubits = 3
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_iqpembedding(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(feature_vector):
qml.IQPEmbedding(feature_vector, wires=range(n_qubits))
return [qml.expval(qml.Z(w)) for w in range(n_qubits)]

X = [1.0, 2.0, 3.0]
X = np.arange(1, n_qubits + 1)

res = qml.QNode(circuit, dev, diff_method=None)(X)
ref = qml.QNode(circuit, dq, diff_method=None)(X)
Expand All @@ -185,19 +194,20 @@ def circuit(feature_vector):
class TestQAOAEmbedding:
"""Test the QAOAEmbedding algorithm."""

def test_qaoaembedding(self):
n_qubits = 2
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_qaoaembedding(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(feature_vector, weights):
qml.QAOAEmbedding(features=feature_vector, weights=weights, wires=range(n_qubits))
return qml.expval(qml.Z(0))

X = [1.0, 2.0]
layer1 = [0.1, -0.3, 1.5]
layer2 = [3.1, 0.2, -2.8]
weights = [layer1, layer2]
X = np.random.rand(n_qubits)
# [1.0, 2.0]
# layer1 = [0.1, -0.3, 1.5]
# layer2 = [3.1, 0.2, -2.8]
weights = np.random.rand(2, 3 if n_qubits == 2 else 2 * n_qubits)

res = qml.QNode(circuit, dev, diff_method=None)(X, weights)
ref = qml.QNode(circuit, dq, diff_method=None)(X, weights)
Expand All @@ -219,15 +229,15 @@ def circuit(weights):
shapes = qml.CVNeuralNetLayers.shape(n_layers=2, n_wires=n_qubits)
weights = [np.random.random(shape) for shape in shapes]

with pytest.raises(qml._device.DeviceError, match=f"not supported on {device_name}"):
with pytest.raises(qml._device.DeviceError, match="not supported"):
_ = qml.QNode(circuit, dev, diff_method=None)(weights)


class TestRandomLayers:
"""Test the RandomLayers algorithm."""

def test_randomlayers(self):
n_qubits = 2
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_randomlayers(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

Expand All @@ -246,16 +256,16 @@ def circuit(weights):
class TestStronglyEntanglingLayers:
"""Test the StronglyEntanglingLayers algorithm."""

def test_stronglyentanglinglayers(self):
n_qubits = 4
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_stronglyentanglinglayers(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(weights):
qml.StronglyEntanglingLayers(weights=weights, wires=range(n_qubits))
return qml.expval(qml.Z(0))

shape = qml.StronglyEntanglingLayers.shape(n_layers=2, n_wires=4)
shape = qml.StronglyEntanglingLayers.shape(n_layers=2, n_wires=n_qubits)
weights = np.random.random(size=shape)

res = qml.QNode(circuit, dev, diff_method=None)(weights)
Expand All @@ -267,8 +277,8 @@ def circuit(weights):
class TestSimplifiedTwoDesign:
"""Test the SimplifiedTwoDesign algorithm."""

def test_simplifiedtwodesign(self):
n_qubits = 3
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_simplifiedtwodesign(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

Expand All @@ -278,10 +288,8 @@ def circuit(init_weights, weights):
)
return [qml.expval(qml.Z(i)) for i in range(n_qubits)]

init_weights = [np.pi, np.pi, np.pi]
weights_layer1 = [[0.0, np.pi], [0.0, np.pi]]
weights_layer2 = [[np.pi, 0.0], [np.pi, 0.0]]
weights = [weights_layer1, weights_layer2]
init_weights = np.random.rand(n_qubits)
weights = np.random.rand(2, n_qubits - 1, 2)

res = qml.QNode(circuit, dev, diff_method=None)(init_weights, weights)
ref = qml.QNode(circuit, dq, diff_method=None)(init_weights, weights)
Expand All @@ -292,16 +300,16 @@ def circuit(init_weights, weights):
class TestBasicEntanglerLayers:
"""Test the BasicEntanglerLayers algorithm."""

def test_basicentanglerlayers(self):
n_qubits = 3
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_basicentanglerlayers(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(weights):
qml.BasicEntanglerLayers(weights=weights, wires=range(n_qubits))
return [qml.expval(qml.Z(i)) for i in range(n_qubits)]

weights = [[np.pi, np.pi, np.pi]]
weights = np.random.rand(1, n_qubits)

res = qml.QNode(circuit, dev, diff_method=None)(weights)
ref = qml.QNode(circuit, dq, diff_method=None)(weights)
Expand All @@ -312,16 +320,16 @@ def circuit(weights):
class TestMottonenStatePreparation:
"""Test the MottonenStatePreparation algorithm."""

def test_mottonenstatepreparation(self):
n_qubits = 3
@pytest.mark.parametrize("n_qubits", range(2, 6, 2))
def test_mottonenstatepreparation(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(state):
qml.MottonenStatePreparation(state_vector=state, wires=range(n_qubits))
return qml.state()

state = np.array([1, 2j, 3, 4j, 5, 6j, 7, 8j])
state = np.random.rand(2**n_qubits) + 1j * np.random.rand(2**n_qubits)
state = state / np.linalg.norm(state)

res = qml.QNode(circuit, dev, diff_method=None)(state)
Expand All @@ -333,8 +341,8 @@ def circuit(state):
class TestArbitraryStatePreparation:
"""Test the ArbitraryStatePreparation algorithm."""

def test_arbitrarystatepreparation(self):
n_qubits = 4
@pytest.mark.parametrize("n_qubits", range(2, 6, 2))
def test_arbitrarystatepreparation(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

Expand All @@ -353,8 +361,8 @@ def circuit(weights):
class TestCosineWindow:
"""Test the CosineWindow algorithm."""

def test_cosinewindow(self):
n_qubits = 2
@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_cosinewindow(self, n_qubits):
dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

Expand Down
Loading