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
Add a few misc template tests.
  • Loading branch information
vincentmr committed Apr 16, 2024
commit 92744f38620a76e66203135d72adf147b803aeab
188 changes: 164 additions & 24 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 @@ -64,27 +64,6 @@ def circuit(omega):
assert np.sum(prob) - prob[index] < 0.05
vincentmr marked this conversation as resolved.
Show resolved Hide resolved


class TestQSVT:
"""Test the QSVT algorithm."""

@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])
shifts = [qml.PCPhase(i + 0.1, dim=1, wires=[0, 1]) for i in range(3)]

def circuit():
qml.QSVT(block_encode, shifts)
return qml.expval(qml.Z(0))

res = qml.QNode(circuit, dev, diff_method=None)()
ref = qml.QNode(circuit, dq, diff_method=None)()

assert np.allclose(res, ref)


class TestAngleEmbedding:
"""Test the AngleEmbedding algorithm."""

Expand Down Expand Up @@ -198,9 +177,6 @@ def circuit(feature_vector, weights):
return qml.state()

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)
Expand Down Expand Up @@ -610,3 +586,167 @@ def circuit(params):
ref = qml.QNode(circuit, dq, diff_method=None)(weights)

assert np.allclose(res, ref)


class TestApproxTimeEvolution:
"""Test the ApproxTimeEvolution algorithm."""

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

coeffs = [1] * n_qubits
obs = [qml.X(i) for i in range(n_qubits)]
hamiltonian = qml.Hamiltonian(coeffs, obs)

def circuit(time):
qml.ApproxTimeEvolution(hamiltonian, time, 1)
return qml.state()

res = qml.QNode(circuit, dev, diff_method=None)(1.3)
ref = qml.QNode(circuit, dq, diff_method=None)(1.3)

assert np.allclose(res, ref)


class TestQDrift:
"""Test the QDrift algorithm."""

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

coeffs = [1] * n_qubits
obs = [qml.X(i) for i in range(n_qubits)]
hamiltonian = qml.Hamiltonian(coeffs, obs)

def circuit(time):
qml.QDrift(hamiltonian, time=time, n=10, seed=10)
return qml.state()

res = qml.QNode(circuit, dev, diff_method=None)(1.3)
ref = qml.QNode(circuit, dq, diff_method=None)(1.3)

assert np.allclose(res, ref)


class TestTrotterProduct:
"""Test the TrotterProduct algorithm."""

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

coeffs = [1] * n_qubits
obs = [qml.X(i) for i in range(n_qubits)]
hamiltonian = qml.Hamiltonian(coeffs, obs)

def circuit(time):
qml.TrotterProduct(hamiltonian, time=time, order=2)
return qml.state()

res = qml.QNode(circuit, dev, diff_method=None)(1.3)
ref = qml.QNode(circuit, dq, diff_method=None)(1.3)

assert np.allclose(res, ref)


class TestQuantumPhaseEstimation:
"""Test the QuantumPhaseEstimation algorithm."""

@pytest.mark.parametrize("n_qubits", range(2, 12, 2))
def test_QuantumPhaseEstimation(self, n_qubits):

phase = 5
target_wires = [0]
unitary = qml.RX(phase, wires=0).matrix()
n_estimation_wires = n_qubits - 1
estimation_wires = range(1, n_estimation_wires + 1)

dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit():
# Start in the |+> eigenstate of the unitary
qml.Hadamard(wires=target_wires)

qml.QuantumPhaseEstimation(
unitary,
target_wires=target_wires,
estimation_wires=estimation_wires,
)

return qml.probs(estimation_wires)

res = qml.QNode(circuit, dev, diff_method=None)()
ref = qml.QNode(circuit, dq, diff_method=None)()

assert np.allclose(res, ref)


class TestQFT:
"""Test the QFT algorithm."""

@pytest.mark.parametrize("n_qubits", range(2, 14, 2))
def test_QFT(self, n_qubits):

dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(basis_state):
qml.BasisState(basis_state, wires=range(n_qubits))
qml.QFT(wires=range(n_qubits))
return qml.state()

basis_state = [0] * n_qubits
basis_state[0] = 1
res = qml.QNode(circuit, dev, diff_method=None)(basis_state)
ref = qml.QNode(circuit, dq, diff_method=None)(basis_state)

assert np.allclose(res, ref)


class TestAQFT:
"""Test the AQFT algorithm."""

@pytest.mark.parametrize("n_qubits", range(2, 14, 2))
def test_AQFT(self, n_qubits):

dev = qml.device(device_name, wires=n_qubits)
dq = qml.device("default.qubit")

def circuit(basis_state):
qml.BasisState(basis_state, wires=range(n_qubits))
qml.AQFT(order=1, wires=range(n_qubits))
return qml.state()

basis_state = [0] * n_qubits
basis_state[0] = 1
res = qml.QNode(circuit, dev, diff_method=None)(basis_state)
ref = qml.QNode(circuit, dq, diff_method=None)(basis_state)

assert np.allclose(res, ref)


class TestQSVT:
"""Test the QSVT algorithm."""

@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])
shifts = [qml.PCPhase(i + 0.1, dim=1, wires=[0, 1]) for i in range(3)]

def circuit():
qml.QSVT(block_encode, shifts)
return qml.expval(qml.Z(0))

res = qml.QNode(circuit, dev, diff_method=None)()
ref = qml.QNode(circuit, dq, diff_method=None)()

assert np.allclose(res, ref)
Loading