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

Place coding examples with legacy-like structure #19

Merged
merged 33 commits into from
Jan 14, 2025

Conversation

Doomsk
Copy link
Member

@Doomsk Doomsk commented Oct 25, 2024

Add example given by João to compare with current Qadence 1 code:

# Qadence 1
import torch
import matplotlib.pyplot as plt

from qadence import RX, Z, FeatureParameter
from qadence import QuantumCircuit, QuantumModel, BackendName


n_qubits = 1
x = FeatureParameter("x")
block = RX(0, x)
circuit = QuantumCircuit(n_qubits, block)
observable = Z(0)
model = QuantumModel(circuit, observable, backend = BackendName.PYQTORCH)


x = torch.arange(0, 2 * torch.pi, 0.1, requires_grad = True)
values = {"x": x}

fx = model.expectation(values).squeeze()
dfdx = torch.autograd.grad(
        outputs=fx,
        inputs=x,
        grad_outputs = torch.ones_like(x),
    )[0]

plt.plot(x.detach(), fx.detach(), label = "f(x)")
plt.plot(x.detach(), dfdx.detach(), label = "df/dx")
plt.legend()

and Qadence 2:

# Qadence 2
import torch
import matplotlib.pyplot as plt

from qadence2_expressions import RX, Z, parameter
from qadence2.extensions.legacy import QuantumModel

x = parameter("x")
expr = RX(x)(0)
model = QuantumModel(expr, backend="pyqtorch")

x = torch.arange(0, 2 * torch.pi, 0.1, requires_grad=True)
values = {"x": x}

fx = model.expectation(values=values, observable=Z(0)).squeeze()
dfdx = torch.autograd.grad(
    outputs=fx,
    inputs=x,
    grad_outputs=torch.ones_like(x),
)[0]

plt.plot(x.detach(), fx.detach(), label="f(x)")
plt.plot(x.detach(), dfdx.detach(), label="df/dx")
plt.legend()

@Doomsk Doomsk self-assigned this Oct 25, 2024
@Doomsk Doomsk marked this pull request as draft October 25, 2024 10:32
@Doomsk Doomsk requested a review from kaosmicadei October 25, 2024 10:33
Signed-off-by: Doomsk <[email protected]>
@jpmoutinho
Copy link
Contributor

One question, if you consider this to be a "legacy-like" structure, is there some specific new structure that should be used instead? If yes, it would be good to have the examples in that format as well.

Copy link
Contributor

@RolandMacDoland RolandMacDoland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an intermediary review. Minor comments there otherwise LGTM.

.pre-commit-config.yaml Outdated Show resolved Hide resolved
docs/getting_started_example.md Outdated Show resolved Hide resolved
docs/getting_started_example.md Outdated Show resolved Hide resolved
examples/code_prototypes/pyq_basic_rx_v1.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
Signed-off-by: Doomsk <[email protected]>
# Conflicts:
#	examples/pyq_compiler.py
#	pyproject.toml
@Doomsk
Copy link
Member Author

Doomsk commented Nov 21, 2024

One question, if you consider this to be a "legacy-like" structure, is there some specific new structure that should be used instead? If yes, it would be good to have the examples in that format as well.

@jpmoutinho I think it makes sense then to address them in the documentation, let me know what you think

Doomsk and others added 5 commits December 6, 2024 09:20
@pimvenderbosch
Copy link
Member

This PR will close #30

@Doomsk Doomsk changed the title [Draft] Place coding examples with legacy-like structure Place coding examples with legacy-like structure Jan 13, 2025
@Doomsk Doomsk marked this pull request as ready for review January 13, 2025 10:41
# Conflicts:
#	pyproject.toml
Copy link
Contributor

@jpmoutinho jpmoutinho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Doomsk! Not much to say, I think this looks good as a starting point and when we can build from here :)

@pimvenderbosch pimvenderbosch merged commit 1a7f171 into main Jan 14, 2025
6 checks passed
@pimvenderbosch pimvenderbosch deleted the em/run_examples branch January 14, 2025 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants