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

test: fourier reuploading dummy test #11

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
threshold: 0.5%

github_checks:
annotations: false
610 changes: 324 additions & 286 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api"
name = "qiboml"
version = "0.0.1"
description = "Quantum Machine Learning using Qibo"
authors = ["Matteo Robbiati <[email protected]>"]
authors = ["The Qibo team"]
readme = "README.md"
packages = [{ include = "qiboml", from = "src" }]

Expand All @@ -32,6 +32,7 @@ pylint = "^2.17"
pytest-cov = "^3.0.0"
pytest-env = "^0.8.1"
pytest-benchmark = { version = "^4.0.0", extras = ["histogram"] }
qibojit = "^0.1.3"

[tool.poe.tasks]
bench = "pytest benchmarks/"
Expand Down
2 changes: 1 addition & 1 deletion src/qiboml/operations/expectation.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def expectation(
nshots=nshots,
backend=backend,
differentiation_rule=differentiation_rule,
exec_backend=exec_backend,
backend=exec_backend,
)

if differentiation_rule is not None:
Expand Down
16 changes: 13 additions & 3 deletions tests/test_models_reuploading.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
from qiboml.models.reuploading.fourier import FourierReuploading
from qiboml.models.reuploading.u3 import ReuploadingU3

NQUBITS = 3
NQUBITS = 2
NLAYERS = 2
DATADIM = (3,)
DATADIM = (2,)


def test_reuloading_u3():
model = ReuploadingU3(nqubits=NQUBITS, nlayers=NLAYERS, data_dimensionality=DATADIM)
init_angles = model.circuit.get_parameters()
model.inject_data((0.4, 0.5, 0.6))
model.inject_data((0.4, 0.5))
new_angles = model.circuit.get_parameters()
assert init_angles != new_angles


def test_reuloading_fourier():
model = FourierReuploading(
nqubits=NQUBITS, nlayers=NLAYERS, data_dimensionality=DATADIM
)
init_angles = model.circuit.get_parameters()
model.inject_data((0.4, 0.5))
new_angles = model.circuit.get_parameters()
assert init_angles != new_angles
61 changes: 61 additions & 0 deletions tests/test_operations_differentiation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import numpy as np
import pytest
from qibo import Circuit, gates, hamiltonians

from qiboml.operations.differentiation import parameter_shift

NQUBITS = 1
GRADS_SCALEF_1 = [-0.41614683654714213, -0.29426025009181417]
GRADS_SCALEF_05 = [-0.20807341827357106, -0.14713012504590708]


def build_circuit(nqubits: int = 1):
"""Helper function to build parametric circuit."""
circuit = Circuit(nqubits)
for q in range(nqubits):
circuit.add(gates.H(q))
circuit.add(gates.RY(q, theta=0.0))
circuit.add(gates.T(q))
circuit.add(gates.RY(q, theta=0.0))
circuit.add(gates.M(*range(nqubits)))
return circuit


@pytest.mark.parametrize("nshots, atol", [(None, 1e-8), (100000, 1e-2)])
@pytest.mark.parametrize(
"scale_factor, grads",
[(1, GRADS_SCALEF_1), (0.5, GRADS_SCALEF_05)],
)
def test_parameter_shift(nshots, atol, scale_factor, grads):
"""Testing parameter shift rule algorithm."""

h = hamiltonians.Z(NQUBITS)

c = build_circuit(1)
nparams = len(c.get_parameters())

# testing parameter out of bounds
with pytest.raises(ValueError):
_ = parameter_shift(circuit=c, hamiltonian=h, parameter_index=5)

# testing hamiltonian type
with pytest.raises(TypeError):
_ = parameter_shift(circuit=c, hamiltonian=c, parameter_index=0, nshots=nshots)

params = np.linspace(0, 2, nparams, dtype=np.float64)
c.set_parameters(params)

psr_grads = []

for p in range(len(params)):
psr_grads.append(
parameter_shift(
hamiltonian=h,
circuit=c,
parameter_index=p,
scale_factor=scale_factor,
nshots=nshots,
)
)

np.testing.assert_allclose(grads, psr_grads, atol=atol)
Loading