File size: 1,740 Bytes
a49d7b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
from typing import Any

class QuantumGate:
    def __init__(self, gate_type: str):
        self.gate_type = gate_type
        self.matrix = self._initialize_matrix()
        
    def _initialize_matrix(self) -> np.ndarray:
        """Initialize gate unitary matrix"""
        if self.gate_type == "H":
            return np.array([[1, 1], [1, -1]]) / np.sqrt(2)
        elif self.gate_type == "CNOT":
            return np.array([[1, 0, 0, 0],
                           [0, 1, 0, 0],
                           [0, 0, 0, 1],
                           [0, 0, 1, 0]])
        elif self.gate_type == "Phase":
            return np.array([[1, 0], [0, 1j]])
        elif self.gate_type == "X":
            return np.array([[0, 1], [1, 0]])
        elif self.gate_type == "Z":
            return np.array([[1, 0], [0, -1]])
        else:
            raise ValueError(f"Unknown gate type: {self.gate_type}")
            
    def apply(self, state: np.ndarray, topology: Any) -> np.ndarray:
        """Apply gate to quantum state"""
        if self.gate_type == "H":
            return self._apply_single_qubit(state)
        elif self.gate_type == "CNOT":
            return self._apply_two_qubit(state)
        else:
            return self._apply_single_qubit(state)

    def _apply_single_qubit(self, state: np.ndarray) -> np.ndarray:
        if len(state.shape) == 1:
            state_2d = state.reshape(-1, 2)
            result = np.dot(state_2d, self.matrix.T)
            return result.flatten()
        return np.dot(state, self.matrix.T)

    def _apply_two_qubit(self, state: np.ndarray) -> np.ndarray:
        return np.dot(state.reshape(-1, 4), self.matrix.T).flatten()