Skip to content

Commit

Permalink
Additional type hinting, documentation, fixed to gates.py, cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
roy-cruz committed Apr 18, 2024
1 parent e040e59 commit 634194a
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 112 deletions.
74 changes: 36 additions & 38 deletions src/qcrypto/gates.py
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
Loading

0 comments on commit 634194a

Please sign in to comment.