sdim.gatedata
1from dataclasses import dataclass 2import numpy as np 3from .tableau.dataclasses import Tableau 4from typing import Optional 5 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 (qudits) 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 defaults: Optional[dict] = None 20 def __str__(self): 21 return f"{self.name} {self.gate_id}" 22 23@dataclass 24class GateData: 25 """ 26 Manages a collection of quantum gates and their aliases. 27 28 This class handles the creation, storage, and retrieval of quantum gates 29 and their associated data for a provided dimension. 30 31 Attributes: 32 gateMap (dict): A dictionary mapping gate names to Gate objects. 33 aliasMap (dict): A dictionary mapping gate aliases to their primary names. 34 num_gates (int): The total number of gates added. 35 dimension (int): The dimension of the quantum system (default is 2 for qubits). 36 """ 37 gateMap: dict 38 aliasMap: dict 39 num_gates: int = 0 40 dimension: int = 2 41 42 def __init__(self, dimension=2): 43 self.gateMap = {} 44 self.aliasMap = {} 45 self.dimension = dimension 46 self.add_gate_data_pauli(dimension) 47 self.add_gate_hada(dimension) 48 self.add_gate_controlled(dimension) 49 self.add_gate_collapsing(dimension) 50 self.add_gate_noise(dimension) 51 self.add_gate_detectors(dimension) 52 # MUL is registered after the detector gates so DETECTOR/LOGICAL_OBSERVABLE/TICK keep ids 19-21. 53 self.add_gate_multiplication(dimension) 54 55 def __str__(self): 56 return "\n".join(str(gate) for gate in self.gateMap.values()) 57 58 def add_gate(self, name, arg_count): 59 gate_id = self.num_gates 60 gate = Gate(name, arg_count, gate_id) 61 self.gateMap[name] = gate 62 self.num_gates += 1 63 64 def add_gate_alias(self, name, list_alias): 65 for alias in list_alias: 66 self.aliasMap[alias] = name 67 68 def add_gate_data_pauli(self, d): 69 self.add_gate("I", 1) 70 self.add_gate("X", 1) 71 self.add_gate("X_INV", 1) 72 self.add_gate("Z", 1) 73 self.add_gate("Z_INV", 1) 74 75 def add_gate_hada(self, d): 76 self.add_gate("H", 1) 77 self.add_gate_alias("H", ["R", "DFT"]) 78 self.add_gate("H_INV", 1) 79 self.add_gate_alias("H_INV", ["R_INV", "DFT_INV", "H_DAG", "R_DAG", "DFT_DAG"]) 80 self.add_gate("P", 1) 81 self.add_gate_alias("P", ["PHASE", "S"]) 82 self.add_gate("P_INV", 1) 83 self.add_gate_alias("P_INV", ["PHASE_INV", "S_INV"]) 84 85 def add_gate_controlled(self, d): 86 self.add_gate("CNOT", 2) 87 self.add_gate_alias("CNOT", ["SUM", "CX", "C"]) 88 self.add_gate("CNOT_INV", 2) 89 self.add_gate_alias("CNOT_INV", ["SUM_INV", "CX_INV", "C_INV"]) 90 self.add_gate("CZ", 2) 91 self.add_gate("CZ_INV", 2) 92 self.add_gate("SWAP", 2) 93 94 def add_gate_collapsing(self, d): 95 self.add_gate("M", 1) 96 self.add_gate_alias("M", ["MEASURE", "COLLAPSE", "MZ"]) 97 self.add_gate("M_X", 1) 98 self.add_gate_alias("M_X", ["MEASURE_X", "MX"]) 99 self.add_gate("RESET", 1) 100 self.add_gate_alias("RESET", ["MR", "MEASURE_RESET", "MEASURE_R"]) 101 102 def add_gate_noise(self, d): 103 self.add_gate("N1", 1) 104 self.add_gate_alias("N1", ["NOISE1"]) 105 self.gateMap["N1"].defaults = {"noise_channel": "d", "prob": 0.01} 106 107 self.add_gate("N2", 2) 108 self.add_gate_alias("N2", ["NOISE2"]) 109 # N2 defaults to two-qudit depolarizing with probability prob. A full prob_dist 110 # over all d**4 Paulis can still be passed, but it is not built by default since 111 # it gets too big quickly (10**24 floats at d = 1000003). 112 self.gateMap["N2"].defaults = {"prob": 0.01} 113 114 def add_gate_detectors(self, d): 115 116 self.add_gate("DETECTOR", 0) 117 self.add_gate_alias("DETECTOR", ["DETECT", "D"]) 118 self.gateMap["DETECTOR"].defaults = {"expr" : "0"} 119 120 self.add_gate("LOGICAL_OBSERVABLE", 0) 121 self.add_gate_alias("LOGICAL_OBSERVABLE", ["LO", "LOGICALOBSERVABLE", "OBSERVABLE"]) 122 self.gateMap["LOGICAL_OBSERVABLE"].defaults = {"expr" : "0"} 123 124 self.add_gate("TICK", 0) 125 126 127 128 def add_gate_multiplication(self, d): 129 self.add_gate("MUL", 1) 130 self.add_gate_alias("MUL", ["MULT", "MULTIPLY"]) 131 132 def get_gate_id(self, gate_name): 133 if gate_name in self.gateMap: 134 return self.gateMap[gate_name].gate_id 135 elif gate_name in self.aliasMap: 136 return self.gateMap[self.aliasMap[gate_name]].gate_id 137 else: 138 return None 139 140 def get_gate_name(self, gate_id): 141 for name, gate in self.gateMap.items(): 142 if gate.gate_id == gate_id: 143 return name 144 raise ValueError(f"Gate ID {gate_id} not found") 145 146 def get_gate_matrix(self, gate_id): 147 for _, gate in self.gateMap.items(): 148 if gate.gate_id == gate_id: 149 return gate.unitary_matrix 150 raise ValueError(f"Gate ID {gate_id} not found")
@dataclass
class
Gate:
7@dataclass 8class Gate: 9 """ 10 Represents a quantum gate. 11 12 Attributes: 13 name (str): The name of the gate. 14 arg_count (int): The number of arguments (qudits) the gate operates on. 15 gate_id (int): A unique identifier for the gate. 16 """ 17 name: str 18 arg_count: int 19 gate_id: int 20 defaults: Optional[dict] = None 21 def __str__(self): 22 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 (qudits) the gate operates on.
- gate_id (int): A unique identifier for the gate.
@dataclass
class
GateData:
24@dataclass 25class GateData: 26 """ 27 Manages a collection of quantum gates and their aliases. 28 29 This class handles the creation, storage, and retrieval of quantum gates 30 and their associated data for a provided dimension. 31 32 Attributes: 33 gateMap (dict): A dictionary mapping gate names to Gate objects. 34 aliasMap (dict): A dictionary mapping gate aliases to their primary names. 35 num_gates (int): The total number of gates added. 36 dimension (int): The dimension of the quantum system (default is 2 for qubits). 37 """ 38 gateMap: dict 39 aliasMap: dict 40 num_gates: int = 0 41 dimension: int = 2 42 43 def __init__(self, dimension=2): 44 self.gateMap = {} 45 self.aliasMap = {} 46 self.dimension = dimension 47 self.add_gate_data_pauli(dimension) 48 self.add_gate_hada(dimension) 49 self.add_gate_controlled(dimension) 50 self.add_gate_collapsing(dimension) 51 self.add_gate_noise(dimension) 52 self.add_gate_detectors(dimension) 53 # MUL is registered after the detector gates so DETECTOR/LOGICAL_OBSERVABLE/TICK keep ids 19-21. 54 self.add_gate_multiplication(dimension) 55 56 def __str__(self): 57 return "\n".join(str(gate) for gate in self.gateMap.values()) 58 59 def add_gate(self, name, arg_count): 60 gate_id = self.num_gates 61 gate = Gate(name, arg_count, gate_id) 62 self.gateMap[name] = gate 63 self.num_gates += 1 64 65 def add_gate_alias(self, name, list_alias): 66 for alias in list_alias: 67 self.aliasMap[alias] = name 68 69 def add_gate_data_pauli(self, d): 70 self.add_gate("I", 1) 71 self.add_gate("X", 1) 72 self.add_gate("X_INV", 1) 73 self.add_gate("Z", 1) 74 self.add_gate("Z_INV", 1) 75 76 def add_gate_hada(self, d): 77 self.add_gate("H", 1) 78 self.add_gate_alias("H", ["R", "DFT"]) 79 self.add_gate("H_INV", 1) 80 self.add_gate_alias("H_INV", ["R_INV", "DFT_INV", "H_DAG", "R_DAG", "DFT_DAG"]) 81 self.add_gate("P", 1) 82 self.add_gate_alias("P", ["PHASE", "S"]) 83 self.add_gate("P_INV", 1) 84 self.add_gate_alias("P_INV", ["PHASE_INV", "S_INV"]) 85 86 def add_gate_controlled(self, d): 87 self.add_gate("CNOT", 2) 88 self.add_gate_alias("CNOT", ["SUM", "CX", "C"]) 89 self.add_gate("CNOT_INV", 2) 90 self.add_gate_alias("CNOT_INV", ["SUM_INV", "CX_INV", "C_INV"]) 91 self.add_gate("CZ", 2) 92 self.add_gate("CZ_INV", 2) 93 self.add_gate("SWAP", 2) 94 95 def add_gate_collapsing(self, d): 96 self.add_gate("M", 1) 97 self.add_gate_alias("M", ["MEASURE", "COLLAPSE", "MZ"]) 98 self.add_gate("M_X", 1) 99 self.add_gate_alias("M_X", ["MEASURE_X", "MX"]) 100 self.add_gate("RESET", 1) 101 self.add_gate_alias("RESET", ["MR", "MEASURE_RESET", "MEASURE_R"]) 102 103 def add_gate_noise(self, d): 104 self.add_gate("N1", 1) 105 self.add_gate_alias("N1", ["NOISE1"]) 106 self.gateMap["N1"].defaults = {"noise_channel": "d", "prob": 0.01} 107 108 self.add_gate("N2", 2) 109 self.add_gate_alias("N2", ["NOISE2"]) 110 # N2 defaults to two-qudit depolarizing with probability prob. A full prob_dist 111 # over all d**4 Paulis can still be passed, but it is not built by default since 112 # it gets too big quickly (10**24 floats at d = 1000003). 113 self.gateMap["N2"].defaults = {"prob": 0.01} 114 115 def add_gate_detectors(self, d): 116 117 self.add_gate("DETECTOR", 0) 118 self.add_gate_alias("DETECTOR", ["DETECT", "D"]) 119 self.gateMap["DETECTOR"].defaults = {"expr" : "0"} 120 121 self.add_gate("LOGICAL_OBSERVABLE", 0) 122 self.add_gate_alias("LOGICAL_OBSERVABLE", ["LO", "LOGICALOBSERVABLE", "OBSERVABLE"]) 123 self.gateMap["LOGICAL_OBSERVABLE"].defaults = {"expr" : "0"} 124 125 self.add_gate("TICK", 0) 126 127 128 129 def add_gate_multiplication(self, d): 130 self.add_gate("MUL", 1) 131 self.add_gate_alias("MUL", ["MULT", "MULTIPLY"]) 132 133 def get_gate_id(self, gate_name): 134 if gate_name in self.gateMap: 135 return self.gateMap[gate_name].gate_id 136 elif gate_name in self.aliasMap: 137 return self.gateMap[self.aliasMap[gate_name]].gate_id 138 else: 139 return None 140 141 def get_gate_name(self, gate_id): 142 for name, gate in self.gateMap.items(): 143 if gate.gate_id == gate_id: 144 return name 145 raise ValueError(f"Gate ID {gate_id} not found") 146 147 def get_gate_matrix(self, gate_id): 148 for _, gate in self.gateMap.items(): 149 if gate.gate_id == gate_id: 150 return gate.unitary_matrix 151 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).
GateData(dimension=2)
43 def __init__(self, dimension=2): 44 self.gateMap = {} 45 self.aliasMap = {} 46 self.dimension = dimension 47 self.add_gate_data_pauli(dimension) 48 self.add_gate_hada(dimension) 49 self.add_gate_controlled(dimension) 50 self.add_gate_collapsing(dimension) 51 self.add_gate_noise(dimension) 52 self.add_gate_detectors(dimension) 53 # MUL is registered after the detector gates so DETECTOR/LOGICAL_OBSERVABLE/TICK keep ids 19-21. 54 self.add_gate_multiplication(dimension)
def
add_gate_hada(self, d):
76 def add_gate_hada(self, d): 77 self.add_gate("H", 1) 78 self.add_gate_alias("H", ["R", "DFT"]) 79 self.add_gate("H_INV", 1) 80 self.add_gate_alias("H_INV", ["R_INV", "DFT_INV", "H_DAG", "R_DAG", "DFT_DAG"]) 81 self.add_gate("P", 1) 82 self.add_gate_alias("P", ["PHASE", "S"]) 83 self.add_gate("P_INV", 1) 84 self.add_gate_alias("P_INV", ["PHASE_INV", "S_INV"])
def
add_gate_noise(self, d):
103 def add_gate_noise(self, d): 104 self.add_gate("N1", 1) 105 self.add_gate_alias("N1", ["NOISE1"]) 106 self.gateMap["N1"].defaults = {"noise_channel": "d", "prob": 0.01} 107 108 self.add_gate("N2", 2) 109 self.add_gate_alias("N2", ["NOISE2"]) 110 # N2 defaults to two-qudit depolarizing with probability prob. A full prob_dist 111 # over all d**4 Paulis can still be passed, but it is not built by default since 112 # it gets too big quickly (10**24 floats at d = 1000003). 113 self.gateMap["N2"].defaults = {"prob": 0.01}
def
add_gate_detectors(self, d):
115 def add_gate_detectors(self, d): 116 117 self.add_gate("DETECTOR", 0) 118 self.add_gate_alias("DETECTOR", ["DETECT", "D"]) 119 self.gateMap["DETECTOR"].defaults = {"expr" : "0"} 120 121 self.add_gate("LOGICAL_OBSERVABLE", 0) 122 self.add_gate_alias("LOGICAL_OBSERVABLE", ["LO", "LOGICALOBSERVABLE", "OBSERVABLE"]) 123 self.gateMap["LOGICAL_OBSERVABLE"].defaults = {"expr" : "0"} 124 125 self.add_gate("TICK", 0)