sdim.gatedata
1from dataclasses import dataclass 2import numpy as np 3from .tableau.dataclasses import Tableau 4 5@dataclass 6class Gate: 7 """ 8 Represents a quantum gate. 9 10 Attributes: 11 name (str): The name of the gate. 12 arg_count (int): The number of arguments (qubits) the gate operates on. 13 gate_id (int): A unique identifier for the gate. 14 """ 15 name: str 16 arg_count: int 17 gate_id: int 18 def __str__(self): 19 return f"{self.name} {self.gate_id}" 20 21@dataclass 22class GateData: 23 """ 24 Manages a collection of quantum gates and their aliases. 25 26 This class handles the creation, storage, and retrieval of quantum gates 27 and their associated data for a provided dimension. 28 29 Attributes: 30 gateMap (dict): A dictionary mapping gate names to Gate objects. 31 aliasMap (dict): A dictionary mapping gate aliases to their primary names. 32 num_gates (int): The total number of gates added. 33 dimension (int): The dimension of the quantum system (default is 2 for qubits). 34 """ 35 gateMap: dict 36 aliasMap: dict 37 num_gates: int = 0 38 dimension: int = 2 39 40 def __init__(self, dimension=2): 41 self.gateMap = {} 42 self.aliasMap = {} 43 self.dimension = dimension 44 self.add_gate_data_pauli(dimension) 45 self.add_gate_hada(dimension) 46 self.add_gate_controlled(dimension) 47 self.add_gate_collapsing(dimension) 48 49 def __str__(self): 50 return "\n".join(str(gate) for gate in self.gateMap.values()) 51 52 def add_gate(self, name, arg_count): 53 gate_id = self.num_gates 54 gate = Gate(name, arg_count, gate_id) 55 self.gateMap[name] = gate 56 self.num_gates += 1 57 58 def add_gate_alias(self, name, list_alias): 59 for alias in list_alias: 60 self.aliasMap[alias] = name 61 62 def add_gate_data_pauli(self, d): 63 self.add_gate("I", 1) 64 self.add_gate("X", 1) 65 self.add_gate("X_INV", 1) 66 self.add_gate("Z", 1) 67 self.add_gate("Z_INV", 1) 68 69 def add_gate_hada(self, d): 70 self.add_gate("H", 1) 71 self.add_gate_alias("H", ["R", "DFT"]) 72 self.add_gate("H_INV", 1) 73 self.add_gate_alias("H_INV", ["R_INV", "DFT_INV", "H_DAG", "R_DAG", "DFT_DAG"]) 74 self.add_gate("P", 1) 75 self.add_gate_alias("P", ["PHASE", "S"]) 76 self.add_gate("P_INV", 1) 77 self.add_gate_alias("P_INV", ["PHASE_INV", "S_INV"]) 78 79 def add_gate_controlled(self, d): 80 self.add_gate("CNOT", 2) 81 self.add_gate_alias("CNOT", ["SUM", "CX", "C"]) 82 self.add_gate("CNOT_INV", 2) 83 self.add_gate_alias("CNOT_INV", ["SUM_INV", "CX_INV", "C_INV"]) 84 self.add_gate("CZ", 2) 85 self.add_gate("CZ_INV", 2) 86 self.add_gate("SWAP", 2) 87 88 def add_gate_collapsing(self, d): 89 self.add_gate("M", 1) 90 self.add_gate_alias("M", ["MEASURE", "COLLAPSE", "MZ"]) 91 self.add_gate("M_X", 1) 92 self.add_gate_alias("M_X", ["MEASURE_X", "MX"]) 93 self.add_gate("RESET", 1) 94 self.add_gate_alias("RESET", ["MR", "MEASURE_RESET", "MEASURE_R"]) 95 96 def get_gate_id(self, gate_name): 97 if gate_name in self.gateMap: 98 return self.gateMap[gate_name].gate_id 99 elif gate_name in self.aliasMap: 100 return self.gateMap[self.aliasMap[gate_name]].gate_id 101 else: 102 return None 103 104 def get_gate_name(self, gate_id): 105 for name, gate in self.gateMap.items(): 106 if gate.gate_id == gate_id: 107 return name 108 raise ValueError(f"Gate ID {gate_id} not found") 109 110 def get_gate_matrix(self, gate_id): 111 for _, gate in self.gateMap.items(): 112 if gate.gate_id == gate_id: 113 return gate.unitary_matrix 114 raise ValueError(f"Gate ID {gate_id} not found")
@dataclass
class
Gate:
6@dataclass 7class Gate: 8 """ 9 Represents a quantum gate. 10 11 Attributes: 12 name (str): The name of the gate. 13 arg_count (int): The number of arguments (qubits) the gate operates on. 14 gate_id (int): A unique identifier for the gate. 15 """ 16 name: str 17 arg_count: int 18 gate_id: int 19 def __str__(self): 20 return f"{self.name} {self.gate_id}"
Represents a quantum gate.
Attributes:
- name (str): The name of the gate.
- arg_count (int): The number of arguments (qubits) the gate operates on.
- gate_id (int): A unique identifier for the gate.
@dataclass
class
GateData:
22@dataclass 23class GateData: 24 """ 25 Manages a collection of quantum gates and their aliases. 26 27 This class handles the creation, storage, and retrieval of quantum gates 28 and their associated data for a provided dimension. 29 30 Attributes: 31 gateMap (dict): A dictionary mapping gate names to Gate objects. 32 aliasMap (dict): A dictionary mapping gate aliases to their primary names. 33 num_gates (int): The total number of gates added. 34 dimension (int): The dimension of the quantum system (default is 2 for qubits). 35 """ 36 gateMap: dict 37 aliasMap: dict 38 num_gates: int = 0 39 dimension: int = 2 40 41 def __init__(self, dimension=2): 42 self.gateMap = {} 43 self.aliasMap = {} 44 self.dimension = dimension 45 self.add_gate_data_pauli(dimension) 46 self.add_gate_hada(dimension) 47 self.add_gate_controlled(dimension) 48 self.add_gate_collapsing(dimension) 49 50 def __str__(self): 51 return "\n".join(str(gate) for gate in self.gateMap.values()) 52 53 def add_gate(self, name, arg_count): 54 gate_id = self.num_gates 55 gate = Gate(name, arg_count, gate_id) 56 self.gateMap[name] = gate 57 self.num_gates += 1 58 59 def add_gate_alias(self, name, list_alias): 60 for alias in list_alias: 61 self.aliasMap[alias] = name 62 63 def add_gate_data_pauli(self, d): 64 self.add_gate("I", 1) 65 self.add_gate("X", 1) 66 self.add_gate("X_INV", 1) 67 self.add_gate("Z", 1) 68 self.add_gate("Z_INV", 1) 69 70 def add_gate_hada(self, d): 71 self.add_gate("H", 1) 72 self.add_gate_alias("H", ["R", "DFT"]) 73 self.add_gate("H_INV", 1) 74 self.add_gate_alias("H_INV", ["R_INV", "DFT_INV", "H_DAG", "R_DAG", "DFT_DAG"]) 75 self.add_gate("P", 1) 76 self.add_gate_alias("P", ["PHASE", "S"]) 77 self.add_gate("P_INV", 1) 78 self.add_gate_alias("P_INV", ["PHASE_INV", "S_INV"]) 79 80 def add_gate_controlled(self, d): 81 self.add_gate("CNOT", 2) 82 self.add_gate_alias("CNOT", ["SUM", "CX", "C"]) 83 self.add_gate("CNOT_INV", 2) 84 self.add_gate_alias("CNOT_INV", ["SUM_INV", "CX_INV", "C_INV"]) 85 self.add_gate("CZ", 2) 86 self.add_gate("CZ_INV", 2) 87 self.add_gate("SWAP", 2) 88 89 def add_gate_collapsing(self, d): 90 self.add_gate("M", 1) 91 self.add_gate_alias("M", ["MEASURE", "COLLAPSE", "MZ"]) 92 self.add_gate("M_X", 1) 93 self.add_gate_alias("M_X", ["MEASURE_X", "MX"]) 94 self.add_gate("RESET", 1) 95 self.add_gate_alias("RESET", ["MR", "MEASURE_RESET", "MEASURE_R"]) 96 97 def get_gate_id(self, gate_name): 98 if gate_name in self.gateMap: 99 return self.gateMap[gate_name].gate_id 100 elif gate_name in self.aliasMap: 101 return self.gateMap[self.aliasMap[gate_name]].gate_id 102 else: 103 return None 104 105 def get_gate_name(self, gate_id): 106 for name, gate in self.gateMap.items(): 107 if gate.gate_id == gate_id: 108 return name 109 raise ValueError(f"Gate ID {gate_id} not found") 110 111 def get_gate_matrix(self, gate_id): 112 for _, gate in self.gateMap.items(): 113 if gate.gate_id == gate_id: 114 return gate.unitary_matrix 115 raise ValueError(f"Gate ID {gate_id} not found")
Manages a collection of quantum gates and their aliases.
This class handles the creation, storage, and retrieval of quantum gates and their associated data for a provided dimension.
Attributes:
- gateMap (dict): A dictionary mapping gate names to Gate objects.
- aliasMap (dict): A dictionary mapping gate aliases to their primary names.
- num_gates (int): The total number of gates added.
- dimension (int): The dimension of the quantum system (default is 2 for qubits).
def
add_gate_hada(self, d):
70 def add_gate_hada(self, d): 71 self.add_gate("H", 1) 72 self.add_gate_alias("H", ["R", "DFT"]) 73 self.add_gate("H_INV", 1) 74 self.add_gate_alias("H_INV", ["R_INV", "DFT_INV", "H_DAG", "R_DAG", "DFT_DAG"]) 75 self.add_gate("P", 1) 76 self.add_gate_alias("P", ["PHASE", "S"]) 77 self.add_gate("P_INV", 1) 78 self.add_gate_alias("P_INV", ["PHASE_INV", "S_INV"])