-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b02222c
commit 364acbf
Showing
2 changed files
with
42 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import qibo | ||
|
||
qibo.set_backend("pytorch") | ||
import torch | ||
|
||
from qibo import gates, models | ||
|
||
torch.set_anomaly_enabled(True) | ||
|
||
# Optimization parameters | ||
nepochs = 1 | ||
optimizer = torch.optim.Adam | ||
target_state = torch.ones(4, dtype=torch.complex128) / 2.0 | ||
|
||
# Define circuit ansatz | ||
params = torch.rand(2, dtype=torch.float64, requires_grad=True) | ||
print(params) | ||
optimizer = optimizer([params]) | ||
c = models.Circuit(2) | ||
c.add(gates.RX(0, params[0])) | ||
c.add(gates.RY(1, params[1])) | ||
gate = gates.RY(0, params[1]) | ||
|
||
print("Gate", gate.matrix()) | ||
print(torch.norm(gate.matrix()).grad) | ||
|
||
# for _ in range(nepochs): | ||
# optimizer.zero_grad() | ||
# c.set_parameters(params) | ||
# final_state = c().state() | ||
# print("state", final_state) | ||
# fidelity = torch.abs(torch.sum(torch.conj(target_state) * final_state)) | ||
# loss = 1 - fidelity | ||
# loss.backward() | ||
# optimizer.step() | ||
# print("state", final_state) | ||
# print("params", params) | ||
# print("loss", loss.grad) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters