psiqit.algorithms
Auto-generated documentation for the psiqit.algorithms module.
psiqit.algorithms
PSIQIT - Python Scientific Quantum Information Toolkit Algorithms Module
This module provides quantum algorithms for various computational tasks.
Submodules:
grover.py - Grover's search algorithm (quadratic speedup for search) deutsch_jozsa.py - Deutsch-Jozsa algorithm (determine if function is constant or balanced) bernstein_vazirani.py - Bernstein-Vazirani algorithm (find hidden string) simon.py - Simon's algorithm (find period of a function) qft.py - Quantum Fourier Transform (key component for many algorithms) qpe.py - Quantum Phase Estimation (estimate eigenvalues of unitary operators) shor.py - Shor's factoring algorithm (exponential speedup for factoring)
grover_search(n_qubits, target, n_iterations=None, shots=1024, oracle_type='phase', return_circuit=False, seed=None)
Grover's search algorithm for unstructured search
Finds a target state in an unsorted database of size N = 2^n using O(√N) queries (quadratic speedup over classical O(N)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits (database size = 2^n) |
required |
target
|
int
|
Target state to search for (0 to 2^n - 1) |
required |
n_iterations
|
Optional[int]
|
Number of Grover iterations (auto-calculated if None) |
None
|
shots
|
int
|
Number of measurement shots |
1024
|
oracle_type
|
str
|
'phase' or 'boolean' (phase oracle is more efficient) |
'phase'
|
return_circuit
|
bool
|
If True, return the circuit as well |
False
|
seed
|
Optional[int]
|
Random seed for reproducibility |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Results including the most likely state and counts |
Example
Search for state |101⟩ in 3-qubit system
result = grover_search(n_qubits=3, target=5, shots=1024) print(result['most_likely']) # 5 print(result['success']) # True if probability > 0.5
Search with custom iterations
result = grover_search(n_qubits=4, target=10, n_iterations=3)
Source code in psiqit\algorithms\grover.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
grover_search_simulated(n_qubits, target, n_iterations=None)
Simulated version of Grover's algorithm using state vectors
This is faster and more accurate for testing, as it doesn't build circuits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
target
|
int
|
Target state to find |
required |
n_iterations
|
Optional[int]
|
Number of iterations (auto-calculated if None) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Results with probabilities |
Example
result = grover_search_simulated(n_qubits=3, target=5) print(result['probability_target']) # ~0.94 print(result['most_likely']) # 5
Source code in psiqit\algorithms\grover.py
grover_with_custom_oracle(n_qubits, oracle, n_iterations=None, shots=1024, return_circuit=False)
Grover's search with a custom oracle function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
oracle
|
Callable
|
Oracle function that takes a circuit and qubits |
required |
n_iterations
|
Optional[int]
|
Number of Grover iterations |
None
|
shots
|
int
|
Number of measurement shots |
1024
|
return_circuit
|
bool
|
If True, return the circuit |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Results |
Example
def my_oracle(circ, qubits): ... # Mark state |101⟩ ... circ.x(qubits[0]) ... circ.x(qubits[2]) ... circ.cz(qubits[0], qubits[1]) ... circ.cz(qubits[1], qubits[2]) ... circ.x(qubits[0]) ... circ.x(qubits[2]) result = grover_with_custom_oracle(3, my_oracle)
Source code in psiqit\algorithms\grover.py
optimal_grover_iterations(n_qubits)
Calculate the optimal number of Grover iterations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Optimal number of iterations |
Example
optimal_grover_iterations(3) # 2 optimal_grover_iterations(4) # 3
Source code in psiqit\algorithms\grover.py
grover_probability(n_qubits, n_iterations, target=0)
Calculate the probability of finding the target state after n_iterations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
n_iterations
|
int
|
Number of Grover iterations |
required |
target
|
int
|
Target state (for reference) |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Probability of target state |
Example
grover_probability(3, 2) # ~0.945
Source code in psiqit\algorithms\grover.py
grover_amplitude(n_qubits, n_iterations, target=0)
Calculate the amplitude of the target state after n_iterations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
n_iterations
|
int
|
Number of Grover iterations |
required |
target
|
int
|
Target state (for reference) |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Amplitude of target state |
Example
grover_amplitude(3, 2) # ~0.972
Source code in psiqit\algorithms\grover.py
deutsch_algorithm(oracle_function=None, function_type=None, shots=1024, return_circuit=False, n_qubits=1)
Deutsch algorithm (1-qubit version of Deutsch-Jozsa)
Source code in psiqit\algorithms\deutsch_jozsa.py
bernstein_vazirani_circuit(hidden_string, include_measurements=True)
Create a Bernstein-Vazirani circuit for a given hidden string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_string
|
str
|
Hidden string s (e.g., '101') |
required |
include_measurements
|
bool
|
Whether to include measurement gates |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
QuantumCircuit |
QuantumCircuit
|
Circuit implementing Bernstein-Vazirani |
Example
circ = bernstein_vazirani_circuit('101') print(circ.draw())
Source code in psiqit\algorithms\bernstein_vazirani.py
bernstein_vazirani_simulated(hidden_string, shots=1024)
Simulate Bernstein-Vazirani algorithm using state vector
This is a faster simulation that doesn't build a circuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_string
|
str
|
Hidden string s |
required |
shots
|
int
|
Number of measurement shots |
1024
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Results |
Example
result = bernstein_vazirani_simulated('101') print(result['found_string']) # '101'
Source code in psiqit\algorithms\bernstein_vazirani.py
bernstein_vazirani_classical(oracle_function, n_qubits)
Classical version of Bernstein-Vazirani (requires n queries)
This is provided for comparison with the quantum algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oracle_function
|
callable
|
Oracle function f(x) that computes s·x (mod 2) |
required |
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Found hidden string |
Example
def oracle(x): ... # s = 101 ... return (x[0] + x[2]) % 2 result = bernstein_vazirani_classical(oracle, 3) print(result) # '101'
Source code in psiqit\algorithms\bernstein_vazirani.py
create_oracle_from_hidden_string(hidden_string)
Create an oracle function from a hidden string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_string
|
str
|
Hidden string s |
required |
Returns:
| Name | Type | Description |
|---|---|---|
callable |
callable
|
Oracle function f(x) = s·x (mod 2) |
Example
oracle = create_oracle_from_hidden_string('101') print(oracle([1, 0, 1])) # 0 (11 + 00 + 1*1 = 2 mod 2 = 0) print(oracle([1, 0, 0])) # 1
Source code in psiqit\algorithms\bernstein_vazirani.py
generate_random_hidden_string(n_qubits, seed=None)
Generate a random non-zero hidden string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
seed
|
Optional[int]
|
Random seed |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Random binary string (non-zero) |
Example
s = generate_random_hidden_string(5) print(s) # e.g., '10110'
Source code in psiqit\algorithms\simon.py
simon_algorithm(oracle_function=None, hidden_string=None, n_qubits=None, shots=1024, max_attempts=10, return_circuit=False, verbose=False)
Simon's algorithm to find the period of a function
Given a function f: {0,1}ⁿ → {0,1}ⁿ such that f(x) = f(x ⊕ s) for some hidden string s, Simon's algorithm finds s using O(n) queries (exponential speedup over classical O(2^{n/2})).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oracle_function
|
Optional[Callable]
|
Oracle function f(x) that returns a bitstring If provided, hidden_string is ignored. |
None
|
hidden_string
|
Optional[str]
|
Hidden string s (e.g., '101') If provided, oracle is constructed automatically. |
None
|
n_qubits
|
Optional[int]
|
Number of qubits (automatically determined if not provided) |
None
|
shots
|
int
|
Number of measurement shots |
1024
|
max_attempts
|
int
|
Maximum number of attempts to find s |
10
|
return_circuit
|
bool
|
If True, return the circuit as well |
False
|
verbose
|
bool
|
Print progress information |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Results including the found string |
Example
Find hidden string '101'
result = simon_algorithm(hidden_string='101') print(result['hidden_string']) # '101' print(result['found_string']) # '101'
With custom oracle
def oracle(x): ... # f(x) = x if x[0] == 0, else x ⊕ s ... s = [1, 0, 1] ... if x[0] == 0: ... return x ... else: ... return [(xi ^ si) for xi, si in zip(x, s)] result = simon_algorithm(oracle_function=oracle, n_qubits=3)
Source code in psiqit\algorithms\simon.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
simon_classical(oracle, n_qubits, max_attempts=20)
Classical version of Simon's algorithm (for comparison)
This is the classical algorithm for finding the period, which requires O(2^{n/2}) queries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oracle
|
Callable
|
Oracle function f(x) |
required |
n_qubits
|
int
|
Number of qubits |
required |
max_attempts
|
int
|
Maximum number of attempts |
20
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: Found period or None if not found |
Example
def oracle(x): ... s = [1, 0, 1] ... if x[0] == 0: ... return x ... else: ... return [(xi ^ si) for xi, si in zip(x, s)] result = simon_classical(oracle, 3) print(result) # '101'
Source code in psiqit\algorithms\simon.py
create_simon_oracle(hidden_string)
Create an oracle for Simon's algorithm
The oracle implements f(x) = x if x[0] == 0, else x ⊕ s
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_string
|
str
|
Hidden string s |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Oracle function |
Example
oracle = create_simon_oracle('101') print(oracle([0, 0, 0])) # [0, 0, 0] print(oracle([1, 0, 0])) # [1, 0, 1]
Source code in psiqit\algorithms\simon.py
is_periodic(oracle, s, n_qubits)
Check if s is a period of the oracle function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oracle
|
Callable
|
Oracle function |
required |
s
|
List[int]
|
Candidate period |
required |
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if s is a period, False otherwise |
Source code in psiqit\algorithms\simon.py
qft_circuit(n_qubits, inverse=False, with_measurements=False)
Create a Quantum Fourier Transform circuit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
inverse
|
bool
|
If True, create inverse QFT (IQFT) |
False
|
with_measurements
|
bool
|
If True, include measurement gates |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
QuantumCircuit |
QuantumCircuit
|
QFT circuit |
Example
circ = qft_circuit(3) print(circ.draw())
Source code in psiqit\algorithms\qft.py
qft_matrix(n_qubits)
Generate the QFT matrix for n qubits
The QFT matrix is a unitary matrix of size 2^n × 2^n: (QFT)_{j,k} = (1/√N) ω^{j*k} where ω = e^{2πi/N} and N = 2^n
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: QFT matrix (2^n × 2^n) |
Example
Q = qft_matrix(2) print(Q) # 4×4 QFT matrix
Source code in psiqit\algorithms\qft.py
iqft_matrix(n_qubits)
Generate the inverse QFT matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Inverse QFT matrix |
Example
Q_inv = iqft_matrix(2) Q = qft_matrix(2) print(np.allclose(Q @ Q_inv, np.eye(4))) # True
Source code in psiqit\algorithms\qft.py
iqft(state, n_qubits=None)
Apply inverse Quantum Fourier Transform to a state
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Union[Ket, ndarray, List]
|
Input state (Ket, list, or numpy array) |
required |
n_qubits
|
Optional[int]
|
Number of qubits (auto-detected if not provided) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Ket |
Ket
|
Inverse QFT-transformed state |
Example
from psiqit.quantum import Ket state = Ket([1, 0, 0, 0]) qft_state = qft(state) original = iqft(qft_state) print(np.allclose(original.data, state.data)) # True
Source code in psiqit\algorithms\qft.py
qft_controlled(n_qubits)
Create a controlled QFT circuit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Name | Type | Description |
|---|---|---|
QuantumCircuit |
QuantumCircuit
|
Controlled QFT circuit |
Note
This is a placeholder for future implementation
Source code in psiqit\algorithms\qft.py
qft_basis_state(n_qubits, state_index)
Apply QFT to a basis state |j⟩ without constructing the full state
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
state_index
|
int
|
Index of the basis state (0 to 2^n - 1) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Ket |
Ket
|
QFT-transformed basis state |
Example
result = qft_basis_state(2, 1) # QFT(|01⟩) print(result) # Fourier basis state
Source code in psiqit\algorithms\qft.py
qft_product_state(n_qubits, amplitudes)
Apply QFT to a product state (tensor product of single-qubit states)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
amplitudes
|
List[complex]
|
List of amplitudes for the product state |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Ket |
Ket
|
QFT-transformed state |
Example
amplitudes = [1/np.sqrt(2), 1/np.sqrt(2)] # |+⟩ result = qft_product_state(2, amplitudes)
Source code in psiqit\algorithms\qft.py
compare_qft_to_fft(n_qubits, state_index=0)
Compare QFT to classical FFT on a basis state
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
state_index
|
int
|
Index of the basis state |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Comparison results |
Example
result = compare_qft_to_fft(3, 2) print(result['max_difference']) # Numerical difference
Source code in psiqit\algorithms\qft.py
qft_circuit_comparison(n_qubits)
Compare QFT circuit complexity
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, int]
|
Circuit statistics |
Example
stats = qft_circuit_comparison(3) print(stats['gates']) # Number of gates in QFT circuit
Source code in psiqit\algorithms\qft.py
qft_phase_estimation_prep(n_qubits)
Create a circuit for QFT-based phase estimation (initialization part)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits for phase estimation |
required |
Returns:
| Name | Type | Description |
|---|---|---|
QuantumCircuit |
QuantumCircuit
|
Phase estimation preparation circuit |
Source code in psiqit\algorithms\qft.py
qft_phase_estimation_measure(n_qubits)
Create a circuit for QFT-based phase estimation (measurement part)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Name | Type | Description |
|---|---|---|
QuantumCircuit |
QuantumCircuit
|
Phase estimation measurement circuit |
Source code in psiqit\algorithms\qft.py
quantum_phase_estimation(unitary, n_qubits, state=None, shots=1024, return_circuit=False, return_all=False)
Quantum Phase Estimation algorithm
Estimates the phase φ such that U|ψ⟩ = e^{2πiφ}|ψ⟩ where U is a unitary operator and |ψ⟩ is an eigenstate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unitary
|
Union[Operator, Callable]
|
Unitary operator (Operator or callable that applies U) |
required |
n_qubits
|
int
|
Number of qubits for phase estimation (precision) |
required |
state
|
Optional[Ket]
|
Eigenstate of U (if None, uses |0...0⟩) |
None
|
shots
|
int
|
Number of measurement shots |
1024
|
return_circuit
|
bool
|
If True, return the circuit |
False
|
return_all
|
bool
|
If True, return all details |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Results including estimated phase |
Example
from psiqit.quantum import pauli_z, zero
Estimate phase of Z gate on |0⟩ (eigenvalue = 1, phase = 0)
result = quantum_phase_estimation(pauli_z(), n_qubits=3, state=zero()) print(result['phase']) # 0.0 print(result['phase_angle']) # 0.0
Estimate phase of Z gate on |1⟩ (eigenvalue = -1, phase = 0.5)
from psiqit.quantum import one result = quantum_phase_estimation(pauli_z(), n_qubits=3, state=one()) print(result['phase']) # 0.5 print(result['phase_angle']) # π
Source code in psiqit\algorithms\qpe.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
qpe_circuit(n_qubits, m_qubits=1, with_measurements=True)
Create a Quantum Phase Estimation circuit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits for phase estimation (precision) |
required |
m_qubits
|
int
|
Number of qubits for the unitary (default: 1) |
1
|
with_measurements
|
bool
|
Include measurement gates |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
QuantumCircuit |
QuantumCircuit
|
QPE circuit |
Example
circ = qpe_circuit(n_qubits=3, m_qubits=1) print(circ.draw())
Source code in psiqit\algorithms\qpe.py
qpe_pauli_z(n_qubits, state, shots=1024)
Run QPE on Pauli-Z gate
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits for phase estimation |
required |
state
|
Ket
|
Eigenstate of Z (|0⟩ or |1⟩) |
required |
shots
|
int
|
Number of measurement shots |
1024
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Results |
Example
from psiqit.quantum import zero, one result = qpe_pauli_z(3, zero()) # phase = 0 print(result['phase']) # 0.0 result = qpe_pauli_z(3, one()) # phase = 0.5 print(result['phase']) # 0.5
Source code in psiqit\algorithms\qpe.py
qpe_rotation_gate(n_qubits, angle, state, shots=1024)
Run QPE on a rotation gate (RZ)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits for phase estimation |
required |
angle
|
float
|
Rotation angle |
required |
state
|
Ket
|
Eigenstate of RZ (|0⟩ or |1⟩) |
required |
shots
|
int
|
Number of measurement shots |
1024
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Results |
Example
from psiqit.quantum import zero result = qpe_rotation_gate(3, np.pi/4, zero()) print(result['phase']) # 0.125 (for |0⟩, phase = 0) result = qpe_rotation_gate(3, np.pi/4, one()) print(result['phase']) # 0.375 (for |1⟩, phase = angle/(2π))
Source code in psiqit\algorithms\qpe.py
phase_to_angle(phase)
Convert phase to angle (in radians)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phase
|
float
|
Phase value (0 to 1) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Angle in radians |
Example
phase_to_angle(0.5) # np.pi
Source code in psiqit\algorithms\qpe.py
angle_to_phase(angle)
Convert angle to phase (0 to 1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
angle
|
float
|
Angle in radians |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Phase value (0 to 1) |
Example
angle_to_phase(np.pi) # 0.5
Source code in psiqit\algorithms\qpe.py
phase_to_eigenvalue(phase)
Convert phase to eigenvalue
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phase
|
float
|
Phase value (0 to 1) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
complex |
complex
|
Eigenvalue e^{2πiφ} |
Example
phase_to_eigenvalue(0.5) # -1
Source code in psiqit\algorithms\qpe.py
eigenvalue_to_phase(eigenvalue)
Convert eigenvalue to phase
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eigenvalue
|
complex
|
Complex eigenvalue |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Phase value (0 to 1) |
Example
eigenvalue_to_phase(-1) # 0.5
Source code in psiqit\algorithms\qpe.py
qpe_accuracy(n_qubits, true_phase)
Calculate the theoretical accuracy of QPE
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits for phase estimation |
required |
true_phase
|
float
|
True phase value |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Accuracy statistics |
Example
result = qpe_accuracy(3, 0.5) print(result['max_error']) # 1/2^3 = 0.125
Source code in psiqit\algorithms\qpe.py
shor_factor(N, max_attempts=10, use_quantum=True, verbose=False)
Factor an integer N using Shor's algorithm
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
int
|
Integer to factor |
required |
max_attempts
|
int
|
Maximum number of attempts |
10
|
use_quantum
|
bool
|
Use quantum algorithm (if False, use classical simulation) |
True
|
verbose
|
bool
|
Print progress information |
False
|
Returns:
| Type | Description |
|---|---|
Tuple[int, int]
|
Tuple[int, int]: Factors (p, q) such that p * q = N |
Example
factors = shor_factor(15) print(factors) # (3, 5) factors = shor_factor(21) print(factors) # (3, 7) factors = shor_factor(35) print(factors) # (5, 7)
Source code in psiqit\algorithms\shor.py
shor_classical(N, max_attempts=10)
Classical version of Shor's algorithm (for comparison)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
int
|
Integer to factor |
required |
max_attempts
|
int
|
Maximum number of attempts |
10
|
Returns:
| Type | Description |
|---|---|
Tuple[int, int]
|
Tuple[int, int]: Factors |
Example
factors = shor_classical(15) print(factors) # (3, 5)
Source code in psiqit\algorithms\shor.py
shor_algorithm(N, use_quantum=True, verbose=True, max_attempts=10)
Run Shor's algorithm and return detailed results
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
int
|
Integer to factor |
required |
use_quantum
|
bool
|
Use quantum algorithm (if False, use classical simulation) |
True
|
verbose
|
bool
|
Print progress |
True
|
max_attempts
|
int
|
Maximum number of attempts |
10
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Detailed results |
Example
result = shor_algorithm(15) print(result['factors']) # (3, 5) print(result['attempts']) # Number of attempts used print(result['method']) # 'quantum' or 'classical'
Source code in psiqit\algorithms\shor.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
shor_steps(N, a, r)
Show the steps of Shor's algorithm for a given a and period
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
int
|
Integer to factor |
required |
a
|
int
|
Random base |
required |
r
|
int
|
Period of a^x mod N |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Step-by-step results |
Example
result = shor_steps(15, 7, 4) print(result['step1']) # "Choose a = 7, gcd(7, 15) = 1" print(result['step2']) # "Find period r = 4" print(result['step3']) # "r is even" print(result['step4']) # "x = 7^2 mod 15 = 4" print(result['step5']) # "Factors: gcd(3, 15) = 3, gcd(5, 15) = 5"
Source code in psiqit\algorithms\shor.py
info()
Get information about the algorithms module
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Module information |
Source code in psiqit\algorithms\__init__.py
test()
Run basic tests for the algorithms module
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Test results |