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

[Draft] Place coding examples with legacy-like structure #19

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from

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
Signed-off-by: Doomsk <[email protected]>
@jpmoutinho
Copy link

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
expr = RX(1.57 * a)(0) * RY(0.707 * a ** 2)(0)
print(f"expression: {str(expr)}")

add_qpu_directives({"digital": True})
Copy link
Contributor

Choose a reason for hiding this comment

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

Same.

n_qubits = 2

# I will just copy the value from C6_DICT[60] since there's not such a dict in qadence2
C6 = 865723.02
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe more comment about what this constant is and units.

Copy link
Member Author

Choose a reason for hiding this comment

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

pyproject.toml Outdated
"qadence2-ir==0.1.1",
"qadence2-expressions==0.1.1",
"pyqtorch==1.3.2",
"qadence2-ir~=0.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it's better to remove pinning the dependencies ?

Signed-off-by: Doomsk <[email protected]>
# Conflicts:
#	examples/pyq_compiler.py
#	pyproject.toml
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.

4 participants