forked from GuillermoFidalgo/QKDP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional type hinting, documentation, fixed to gates.py, cleaning code
- Loading branch information
Showing
2 changed files
with
169 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
import numpy as np | ||
|
||
Pauli_gates = [ | ||
np.array([[0, 1], [1, 0]], dtype=np.complex_), | ||
np.array([[0, -1j], [1j, 0]], dtype=np.complex_), | ||
np.array([[1, 0], [0, -1]], dtype=np.complex_), | ||
] | ||
|
||
H_gate = (1 / np.sqrt(2)) * np.array([[1, 1], [1, -1]], dtype=np.complex_) | ||
|
||
|
||
def tensor_power(gate, N): | ||
import numpy.typing as npt | ||
from typing import Union, Dict | ||
|
||
Pauli: Dict[str, npt.NDArray[np.complex_]] = { | ||
"x": np.array([[0, 1], [1, 0]], dtype=np.complex_), | ||
"y": np.array([[0, -1j], [1j, 0]], dtype=np.complex_), | ||
"z": np.array([[1, 0], [0, -1]], dtype=np.complex_), | ||
} | ||
|
||
H_gate: npt.NDArray[np.complex_] = (1 / np.sqrt(2)) * np.array([[1, 1], [1, -1]], dtype=np.complex_) | ||
|
||
def Phase_shift(phase: float) -> npt.NDArray[np.complex_]: | ||
"""Generates a phase shift gate for a given phase. | ||
Args: | ||
phase (float): The phase angle in radians. | ||
Returns: | ||
NDArray: A 2x2 numpy array representing the phase shift gate. | ||
""" | ||
phase_shift_gate = np.array( | ||
[1, 0], | ||
[0, np.e**(1j * phase)] | ||
) | ||
return phase_shift_gate | ||
|
||
def tensor_power(gate: npt.NDArray[np.complex_], N: int) -> npt.NDArray[np.complex_]: | ||
"""Computes the tensor power of a 2x2 gate matrix. | ||
Args: | ||
gate (NDArray): A 2x2 numpy array representing a quantum gate. | ||
N (int): The power to which the gate matrix is to be raised, tensor-wise. | ||
Returns: | ||
NDArray: A numpy array representing the N-th tensor power of the gate. | ||
""" | ||
result = gate | ||
for _ in range(N - 1): | ||
result = np.kron(result, gate) | ||
return result | ||
|
||
|
||
def Gate(state, gate): | ||
N = int(np.log2(len(state))) | ||
gate = tensor_power(gate, N) | ||
new_state = np.dot(gate, state) | ||
return new_state | ||
|
||
|
||
def Hadamard(state): | ||
N = int(np.log2(len(state))) | ||
gate = tensor_power(H_gate, N) | ||
new_state = np.dot(gate, state) | ||
return new_state | ||
|
||
|
||
def Pauli(state, i=0): | ||
# N = int(np.log2(len(state))) | ||
# gate = Pauli_gates[i] | ||
# new_state = np.dot(gate, state) | ||
# return new_state | ||
pass | ||
|
||
|
||
def Rot(state, thetax=0, thetay=0, thetaz=0): | ||
# Rx = np.array([np.cos(thetax/2), -1j * np.sin(thetax/2)], [-1j * np.sin(thetax/2), np.cos(thetax/2)]) | ||
pass |
Oops, something went wrong.