-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
19 changed files
with
783 additions
and
612 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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Backend encoders.""" | ||
|
||
from typing import Union | ||
|
||
import torch | ||
from qiskit.circuit.library import get_standard_gate_name_mapping | ||
from qiskit.providers import BackendV1, BackendV2 | ||
from torch_geometric.data import Data | ||
|
||
from blackwater.data.core import BackendEncoder | ||
from blackwater.data.encoders.graph_utils import backend_to_json_graph | ||
|
||
N_QUBIT_PROPERTIES = 2 | ||
ALL_INSTRUCTIONS = list(get_standard_gate_name_mapping().keys()) | ||
|
||
|
||
# pylint: disable=no-member | ||
class DefaultPyGBackendEncoder(BackendEncoder): | ||
"""Default pytorch geometric backend encoder. | ||
Turns backend into pyg data. | ||
""" | ||
|
||
def encode(self, backend: Union[BackendV1, BackendV2], **kwargs): # type: ignore | ||
backend_graph = backend_to_json_graph(backend) | ||
backend_nodes = torch.tensor(backend_graph.nodes, dtype=torch.float) | ||
backend_edges = torch.transpose( | ||
torch.tensor(backend_graph.edges, dtype=torch.float), 0, 1 | ||
) | ||
backend_edge_features = torch.tensor( | ||
backend_graph.edge_features, dtype=torch.float | ||
) | ||
return Data( | ||
x=backend_nodes, edge_index=backend_edges, edge_attr=backend_edge_features | ||
) |
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,95 @@ | ||
"""Circuit encoders.""" | ||
|
||
from typing import Optional | ||
|
||
import numpy as np | ||
import torch | ||
from qiskit import QuantumCircuit | ||
from qiskit.providers import BackendV2 | ||
from torch_geometric.data import Data | ||
|
||
from blackwater.data.core import CircuitEncoder | ||
from blackwater.data.encoders.graph_utils import ( | ||
DefaultNodeEncoder, | ||
circuit_to_json_graph, | ||
BackendNodeEncoder, | ||
) | ||
|
||
|
||
# pylint: disable=no-member | ||
class DefaultCircuitEncoder(CircuitEncoder): | ||
"""Default circuit encoder to transform circuit into numpy array for training | ||
Returns: | ||
numpy array where: | ||
- first element - depth of circuit | ||
- second element - 2q depth of circuit | ||
- 3rd - number of 1q gates | ||
- 4th - number of 2q gates | ||
- 5th - num qubits | ||
""" | ||
|
||
def encode(self, circuit: QuantumCircuit, **kwargs) -> np.ndarray: # type: ignore | ||
"""Encodes circuit. | ||
Args: | ||
circuit: circuit to encoder | ||
**kwargs: other arguments | ||
Returns: | ||
numpy array | ||
""" | ||
depth = circuit.depth() | ||
two_qubit_depth = circuit.depth(lambda x: x[0].num_qubits == 2) | ||
|
||
num_one_q_gates = 0 | ||
num_two_q_gates = 0 | ||
for instr in circuit._data: | ||
num_qubits = len(instr.qubits) | ||
if num_qubits == 1: | ||
num_one_q_gates += 1 | ||
if num_qubits == 2: | ||
num_two_q_gates += 1 | ||
|
||
return np.array( | ||
[ | ||
depth, | ||
two_qubit_depth, | ||
num_one_q_gates, | ||
num_two_q_gates, | ||
circuit.num_qubits, | ||
] | ||
) | ||
|
||
|
||
class DefaultPyGCircuitEncoder(CircuitEncoder): | ||
"""Default pytorch geometric circuit encoder. | ||
Turns circuit into pyg data. | ||
""" | ||
|
||
def __init__(self, backend: Optional[BackendV2]): | ||
"""Constructor. | ||
Args: | ||
backend: optional backend. Will be used for node data encoding. | ||
""" | ||
self.backend = backend | ||
|
||
def encode(self, circuit: QuantumCircuit, **kwargs): # type: ignore | ||
node_encoder = ( | ||
DefaultNodeEncoder() | ||
if self.backend is None | ||
else BackendNodeEncoder(self.backend) | ||
) | ||
circuit_graph = circuit_to_json_graph(circuit, node_encoder=node_encoder) | ||
circuit_nodes = torch.tensor(circuit_graph.nodes, dtype=torch.float) | ||
circuit_edges = torch.transpose( | ||
torch.tensor(circuit_graph.edges, dtype=torch.long), 0, 1 | ||
) | ||
circuit_edge_features = torch.tensor( | ||
circuit_graph.edge_features, dtype=torch.float | ||
) | ||
return Data( | ||
x=circuit_nodes, edge_index=circuit_edges, edge_attr=circuit_edge_features | ||
) |
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.