Skip to content

Commit 24deb31

Browse files
authored
Create utils.py
1 parent acbd194 commit 24deb31

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/quantum_consensus/utils.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# src/quantum_consensus/utils.py
2+
3+
import numpy as np
4+
import random
5+
import logging
6+
7+
# Set up logging for the utility functions
8+
logger = logging.getLogger(__name__)
9+
10+
def generate_random_state(dimensions=2):
11+
"""
12+
Generate a random quantum state.
13+
14+
Parameters:
15+
- dimensions (int): The dimensions of the quantum state (default is 2 for a qubit).
16+
17+
Returns:
18+
- np.ndarray: A normalized random quantum state.
19+
"""
20+
state = np.random.rand(dimensions)
21+
normalized_state = state / np.linalg.norm(state)
22+
logger.info(f"Generated random quantum state: {normalized_state}")
23+
return normalized_state
24+
25+
def measure_state(state):
26+
"""
27+
Measure a quantum state.
28+
29+
Parameters:
30+
- state (np.ndarray): The quantum state to measure.
31+
32+
Returns:
33+
- int: The result of the measurement (0 or 1 for a qubit).
34+
"""
35+
probabilities = np.abs(state) ** 2
36+
measurement_result = np.random.choice(len(state), p=probabilities)
37+
logger.info(f"Measured state: {state}, Result: {measurement_result}")
38+
return measurement_result
39+
40+
def entangle_states(state1, state2):
41+
"""
42+
Create an entangled state from two quantum states.
43+
44+
Parameters:
45+
- state1 (np.ndarray): The first quantum state.
46+
- state2 (np.ndarray): The second quantum state.
47+
48+
Returns:
49+
- np.ndarray: The resulting entangled state.
50+
"""
51+
# Simple tensor product to create an entangled state
52+
entangled_state = np.kron(state1, state2)
53+
logger.info(f"Created entangled state from {state1} and {state2}: {entangled_state}")
54+
return entangled_state
55+
56+
def apply_unitary(state, unitary):
57+
"""
58+
Apply a unitary operation to a quantum state.
59+
60+
Parameters:
61+
- state (np.ndarray): The quantum state to which the unitary is applied.
62+
- unitary (np.ndarray): The unitary matrix to apply.
63+
64+
Returns:
65+
- np.ndarray: The resulting quantum state after applying the unitary.
66+
"""
67+
new_state = np.dot(unitary, state)
68+
logger.info(f"Applied unitary: {unitary} to state: {state}, Resulting state: {new_state}")
69+
return new_state
70+
71+
def random_unitary(dimensions=2):
72+
"""
73+
Generate a random unitary matrix.
74+
75+
Parameters:
76+
- dimensions (int): The dimensions of the unitary matrix (default is 2 for a qubit).
77+
78+
Returns:
79+
- np.ndarray: A random unitary matrix.
80+
"""
81+
# Generate a random unitary matrix using QR decomposition
82+
random_matrix = np.random.rand(dimensions, dimensions)
83+
q, r = np.linalg.qr(random_matrix)
84+
unitary = q * (np.diag(np.sign(np.diag(r)))) # Ensure the unitary is properly normalized
85+
logger.info(f"Generated random unitary matrix: {unitary}")
86+
return unitary

0 commit comments

Comments
 (0)