-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: drafting reuploading training-encoding layer
- Loading branch information
1 parent
46de4e9
commit 66b4a2e
Showing
4 changed files
with
272 additions
and
69 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
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 |
---|---|---|
@@ -1,23 +1,34 @@ | ||
import random | ||
from typing import List, Optional | ||
|
||
import numpy as np | ||
from qibo import Circuit, gates | ||
|
||
|
||
def ReuploadingCircuit( | ||
nqubits: int, qubits: list[int] = None, nlayers: int = 1 | ||
) -> Circuit: | ||
def entangling_circuit(nqubits: int, entangling_gate: gates.Gate = gates.CNOT): | ||
"""Construct entangling layer.""" | ||
circuit = Circuit(nqubits) | ||
for q in range(nqubits): | ||
circuit.add(entangling_gate(q0=q % nqubits, q1=(q + 1) % nqubits)) | ||
return circuit | ||
|
||
|
||
def layered_ansatz( | ||
nqubits: int, | ||
nlayers: int = 1, | ||
qubits: list[int] = None, | ||
gates_list: Optional[List[gates.Gate]] = [gates.RY, gates.RZ], | ||
entanglement: bool = True, | ||
): | ||
if qubits is None: | ||
qubits = list(range(nqubits)) | ||
|
||
circuit = Circuit(nqubits) | ||
|
||
for _ in range(nlayers): | ||
for l in range(nlayers): | ||
for q in qubits: | ||
circuit.add(gates.RY(q, theta=random.random() * np.pi, trainable=True)) | ||
circuit.add(gates.RZ(q, theta=random.random() * np.pi, trainable=True)) | ||
for i, q in enumerate(qubits[:-2]): | ||
circuit.add(gates.CNOT(q0=q, q1=qubits[i + 1])) | ||
circuit.add(gates.CNOT(q0=qubits[-1], q1=qubits[0])) | ||
for gate in gates_list: | ||
circuit.add(gate(q, theta=random.random(), trainable=True)) | ||
if entanglement: | ||
circuit += entangling_circuit(nqubits, gates.CNOT) | ||
|
||
return circuit |
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
Oops, something went wrong.