psiqit.visualization
Auto-generated documentation for the psiqit.visualization module.
psiqit.visualization
PSIQIT - Python Scientific Quantum Information Toolkit Visualization Module
This module provides visualization tools for quantum states and circuits.
Submodules:
bloch.py - Bloch sphere visualization circuit_drawer.py - Quantum circuit drawing wigner.py - Wigner function visualization husimi.py - Husimi Q-function visualization animation.py - Animation tools symbolic_plot.py - Symbolic equation rendering
state_to_bloch(state)
Convert a single qubit state to Bloch sphere coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Union[Ket, ndarray, List]
|
Single qubit state (Ket, list, or numpy array) |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float]
|
Tuple[float, float, float]: (x, y, z) coordinates |
Example
from psiqit.quantum import zero, plus x, y, z = state_to_bloch(zero()) print(x, y, z) # 0.0, 0.0, 1.0 x, y, z = state_to_bloch(plus()) print(x, y, z) # 1.0, 0.0, 0.0
Source code in psiqit\visualization\bloch.py
bloch_to_state(x, y, z)
Create a single qubit state from Bloch sphere coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
x-coordinate |
required |
y
|
float
|
y-coordinate |
required |
z
|
float
|
z-coordinate |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Ket |
Ket
|
Single qubit state |
Example
state = bloch_to_state(0, 0, 1) # |0⟩ state = bloch_to_state(1, 0, 0) # |+⟩
Source code in psiqit\visualization\bloch.py
spherical_to_bloch(theta, phi)
Convert spherical coordinates to Bloch sphere coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
Polar angle (0 to π) |
required |
phi
|
float
|
Azimuthal angle (0 to 2π) |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float]
|
Tuple[float, float, float]: (x, y, z) coordinates |
Example
x, y, z = spherical_to_bloch(np.pi/2, 0) # |+⟩ print(x, y, z) # 1.0, 0.0, 0.0
Source code in psiqit\visualization\bloch.py
bloch_to_spherical(x, y, z)
Convert Bloch sphere coordinates to spherical coordinates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
x-coordinate |
required |
y
|
float
|
y-coordinate |
required |
z
|
float
|
z-coordinate |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple[float, float]: (theta, phi) angles |
Example
theta, phi = bloch_to_spherical(1, 0, 0) # |+⟩ print(theta, phi) # π/2, 0
Source code in psiqit\visualization\bloch.py
bloch_sphere(state=None, points=None, title='Bloch Sphere', figsize=(8, 8), show_axes=True, show_labels=True, show_grid=True, save_path=None, colors=None, markers=None, labels=None)
Plot a Bloch sphere with a state vector and/or points
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Optional[Union[Ket, ndarray, List]]
|
Single qubit state to display as a vector |
None
|
points
|
Optional[List[Tuple[float, float, float]]]
|
List of (x, y, z) points to display on the sphere |
None
|
title
|
str
|
Title of the plot |
'Bloch Sphere'
|
figsize
|
Tuple[float, float]
|
Figure size (width, height) |
(8, 8)
|
show_axes
|
bool
|
Show x, y, z axes |
True
|
show_labels
|
bool
|
Show axis labels |
True
|
show_grid
|
bool
|
Show grid on the sphere |
True
|
save_path
|
Optional[str]
|
Path to save the figure (if None, displays) |
None
|
colors
|
Optional[List[str]]
|
List of colors for points |
None
|
markers
|
Optional[List[str]]
|
List of markers for points |
None
|
labels
|
Optional[List[str]]
|
List of labels for points |
None
|
Example
from psiqit.quantum import zero, plus, minus bloch_sphere(zero(), title="|0⟩ State") bloch_sphere(points=[(1,0,0), (0,1,0), (0,0,1)], ... labels=['X', 'Y', 'Z'])
Source code in psiqit\visualization\bloch.py
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
plot_multiple_states(states, colors=None, labels=None, title='Multiple States on Bloch Sphere', figsize=(8, 8), show_vectors=True, save_path=None)
Plot multiple states on the Bloch sphere
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
List[Union[Ket, ndarray, List, Tuple[float, float, float]]]
|
List of states (Ket, coordinates, or state vectors) |
required |
colors
|
Optional[List[str]]
|
List of colors for each state |
None
|
labels
|
Optional[List[str]]
|
List of labels for each state |
None
|
title
|
str
|
Title of the plot |
'Multiple States on Bloch Sphere'
|
figsize
|
Tuple[float, float]
|
Figure size |
(8, 8)
|
show_vectors
|
bool
|
Show state vectors |
True
|
save_path
|
Optional[str]
|
Path to save the figure |
None
|
Example
from psiqit.quantum import zero, plus, minus states = [zero(), plus(), minus()] labels = ['|0⟩', '|+⟩', '|-⟩'] plot_multiple_states(states, labels=labels)
Source code in psiqit\visualization\bloch.py
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
animate_bloch(states, interval=200, filename=None, title='Bloch Sphere Animation', figsize=(6, 6), color='purple')
Create an animation of states on the Bloch sphere
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
List[Union[Ket, ndarray, List, Tuple[float, float, float]]]
|
List of states to animate |
required |
interval
|
int
|
Time between frames in milliseconds |
200
|
filename
|
Optional[str]
|
Path to save the animation (if None, displays) |
None
|
title
|
str
|
Title of the animation |
'Bloch Sphere Animation'
|
figsize
|
Tuple[float, float]
|
Figure size |
(6, 6)
|
color
|
str
|
Color of the state vector |
'purple'
|
Example
from psiqit.quantum import zero, plus, minus states = [zero(), plus(), minus(), zero()] animate_bloch(states, interval=500)
Source code in psiqit\visualization\bloch.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
get_bloch_coordinates(state)
Get Bloch sphere coordinates with additional information
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Union[Ket, ndarray, List]
|
Single qubit state |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, float]
|
Bloch coordinates and angles |
Example
from psiqit.quantum import plus info = get_bloch_coordinates(plus()) print(info['x']) # 1.0 print(info['theta']) # π/2
Source code in psiqit\visualization\bloch.py
bloch_distance(state1, state2)
Compute the Euclidean distance between two states on the Bloch sphere
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state1
|
Union[Ket, ndarray, List, Tuple[float, float, float]]
|
First state |
required |
state2
|
Union[Ket, ndarray, List, Tuple[float, float, float]]
|
Second state |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Distance between the states |
Example
from psiqit.quantum import zero, one dist = bloch_distance(zero(), one()) # 2.0
Source code in psiqit\visualization\bloch.py
draw_circuit(circuit, style='ascii', show_wires=True, qubit_labels=None)
Draw a quantum circuit in various styles
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
QuantumCircuit
|
QuantumCircuit object |
required |
style
|
str
|
'ascii', 'unicode', or 'text' |
'ascii'
|
show_wires
|
bool
|
Show wire lines |
True
|
qubit_labels
|
Optional[List[str]]
|
Custom labels for qubits |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Circuit diagram |
Example
from psiqit.circuits import QuantumCircuit circ = QuantumCircuit(2) circ.h(0).cx(0, 1) print(draw_circuit(circ)) q0: ── H ── ● ── q1: ──────── X ──
Source code in psiqit\visualization\circuit_drawer.py
circuit_to_text(circuit, show_wires=True)
Convert circuit to a simple text representation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
QuantumCircuit
|
QuantumCircuit object |
required |
show_wires
|
bool
|
Show wire lines |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Text representation |
Example
print(circuit_to_text(circuit)) Circuit: 2 qubits, 2 gates Gate 1: H on [0] Gate 2: CNOT on [0, 1]
Source code in psiqit\visualization\circuit_drawer.py
circuit_statistics(circuit)
Get statistics about a quantum circuit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
QuantumCircuit
|
QuantumCircuit object |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Any]
|
Circuit statistics |
Example
stats = circuit_statistics(circuit) print(stats['gate_counts']) {'H': 1, 'CNOT': 1}
Source code in psiqit\visualization\circuit_drawer.py
draw_circuit_latex(circuit, style='quantikz')
Generate LaTeX code for a circuit diagram
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
QuantumCircuit
|
QuantumCircuit object |
required |
style
|
str
|
'quantikz' or 'qcircuit' |
'quantikz'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
LaTeX code |
Example
print(draw_circuit_latex(circuit)) \begin{quantikz} \lstick{q0} & \gate{H} & \ctrl{1} & \qw \ \lstick{q1} & \qw & \targ{} & \qw \ \end{quantikz}
Source code in psiqit\visualization\circuit_drawer.py
draw_circuit_qiskit(circuit, style='mpl')
Draw circuit using Qiskit (if available)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
QuantumCircuit
|
QuantumCircuit object |
required |
style
|
str
|
'mpl', 'text', or 'latex' |
'mpl'
|
Returns:
| Type | Description |
|---|---|
|
Qiskit circuit drawing |
Example
draw_circuit_qiskit(circuit, style='mpl')
Source code in psiqit\visualization\circuit_drawer.py
wigner_function(state, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Wigner function for a quantum state
The Wigner function is defined as: W(x, p) = (1/πħ) ∫ dy ⟨x+y|ρ|x-y⟩ e^{-2ipy/ħ}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Union[Ket, ndarray]
|
Quantum state (Ket or density matrix) |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(W, x, p) where W is the Wigner function |
Example
from psiqit.quantum import coherent_state alpha = 1.0 + 1j*0.5 state = coherent_state(alpha, n_levels=20) W, x, p = wigner_function(state, (-3, 3), (-3, 3)) plot_wigner(W, x, p, title="Coherent State")
Source code in psiqit\visualization\wigner.py
wigner_function_analytic(psi_func, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Wigner function from an analytic wavefunction
W(x, p) = (1/πħ) ∫ dy ψ(x+y) ψ*(x-y) e^{-2ipy/ħ}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psi_func
|
Callable[[ndarray], ndarray]
|
Wavefunction ψ(x) as a callable |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(W, x, p) |
Example
def psi(x): ... return np.exp(-x**2/2) / np.sqrt(np.sqrt(np.pi)) W, x, p = wigner_function_analytic(psi, (-3, 3), (-3, 3)) plot_wigner(W, x, p)
Source code in psiqit\visualization\wigner.py
wigner_function_gaussian(x0=0.0, p0=0.0, sigma=1.0, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Wigner function for a Gaussian state
W(x, p) = (1/πħ) exp(-(x-x0)²/(2σ²) - (p-p0)²/(2σ²))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x0
|
float
|
Center position |
0.0
|
p0
|
float
|
Center momentum |
0.0
|
sigma
|
float
|
Width parameter |
1.0
|
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(W, x, p) |
Example
W, x, p = wigner_function_gaussian(x0=0, p0=0, sigma=1.0) plot_wigner(W, x, p, title="Gaussian State")
Source code in psiqit\visualization\wigner.py
wigner_function_coherent_state(alpha, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Wigner function for a coherent state |α⟩
W(x, p) = (1/πħ) exp(-(x-x0)²/(2σ²) - (p-p0)²/(2σ²)) where x0 = √(2ħ) Re(α) and p0 = √(2ħ) Im(α)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
complex
|
Complex amplitude |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(W, x, p) |
Example
alpha = 1.0 + 1j*0.5 W, x, p = wigner_function_coherent_state(alpha, (-3, 3), (-3, 3)) plot_wigner(W, x, p, title="Coherent State")
Source code in psiqit\visualization\wigner.py
wigner_function_squeezed_state(r, phi=0.0, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Wigner function for a squeezed state
W(x, p) = (1/πħ) exp(-(x²/(2σ²_x) + p²/(2σ²_p))) where σ_x = sqrt(ħ/2) e^r and σ_p = sqrt(ħ/2) e^{-r}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
float
|
Squeezing parameter |
required |
phi
|
float
|
Squeezing phase |
0.0
|
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(W, x, p) |
Example
W, x, p = wigner_function_squeezed_state(r=0.5, phi=0, (-3, 3), (-3, 3)) plot_wigner(W, x, p, title="Squeezed State")
Source code in psiqit\visualization\wigner.py
plot_wigner(W, x, p, title='Wigner Function', cmap='RdBu', save_path=None, figsize=(8, 6), show_colorbar=True, show_contours=False, contour_levels=10)
Plot the Wigner function as a 2D colormap
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
W
|
ndarray
|
Wigner function values |
required |
x
|
ndarray
|
x-axis (position) values |
required |
p
|
ndarray
|
y-axis (momentum) values |
required |
title
|
str
|
Title of the plot |
'Wigner Function'
|
cmap
|
str
|
Colormap name |
'RdBu'
|
save_path
|
Optional[str]
|
Path to save the figure |
None
|
figsize
|
Tuple[float, float]
|
Figure size (width, height) |
(8, 6)
|
show_colorbar
|
bool
|
Show colorbar |
True
|
show_contours
|
bool
|
Show contour lines |
False
|
contour_levels
|
int
|
Number of contour levels |
10
|
Example
W, x, p = wigner_function_gaussian() plot_wigner(W, x, p, title="Gaussian State")
Save to file
plot_wigner(W, x, p, save_path="wigner.png")
Source code in psiqit\visualization\wigner.py
plot_wigner_3d(W, x, p, title='Wigner Function', save_path=None, figsize=(10, 8), elevation=30, azimuth=45)
Plot the Wigner function as a 3D surface
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
W
|
ndarray
|
Wigner function values |
required |
x
|
ndarray
|
x-axis (position) values |
required |
p
|
ndarray
|
y-axis (momentum) values |
required |
title
|
str
|
Title of the plot |
'Wigner Function'
|
save_path
|
Optional[str]
|
Path to save the figure |
None
|
figsize
|
Tuple[float, float]
|
Figure size (width, height) |
(10, 8)
|
elevation
|
float
|
View elevation angle |
30
|
azimuth
|
float
|
View azimuthal angle |
45
|
Example
W, x, p = wigner_function_gaussian() plot_wigner_3d(W, x, p, title="Gaussian State")
Source code in psiqit\visualization\wigner.py
wigner_negativity(W)
Compute the negativity of the Wigner function
Negativity = ∫ |W(x, p)| dx dp - 1
A non-zero negativity indicates non-classicality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
W
|
ndarray
|
Wigner function values |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Negativity |
Example
W, x, p = wigner_function_coherent_state(alpha) neg = wigner_negativity(W) print(f"Negativity: {neg:.6f}") # ~0 for coherent states
Source code in psiqit\visualization\wigner.py
wigner_marginals(W, x, p)
Compute the marginals of the Wigner function
The position marginal is: P(x) = ∫ W(x, p) dp The momentum marginal is: Q(p) = ∫ W(x, p) dx
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
W
|
ndarray
|
Wigner function values |
required |
x
|
ndarray
|
x-axis (position) values |
required |
p
|
ndarray
|
y-axis (momentum) values |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray]
|
(P_x, Q_p) position and momentum marginals |
Source code in psiqit\visualization\wigner.py
wigner_variance(W, x, p)
Compute the variance of the Wigner function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
W
|
ndarray
|
Wigner function values |
required |
x
|
ndarray
|
x-axis (position) values |
required |
p
|
ndarray
|
y-axis (momentum) values |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[float, float]
|
(var_x, var_p) |
Source code in psiqit\visualization\wigner.py
husimi_function_gaussian(x0=0.0, p0=0.0, sigma=1.0, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Husimi Q-function for a Gaussian state
The Husimi Q-function is defined as: Q(α) = (1/π) ⟨α|ρ|α⟩
For a Gaussian state with position x0 and momentum p0: Q(x, p) = (1/π) exp(-(x-x0)²/(2σ²) - (p-p0)²/(2σ²))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x0
|
float
|
Center position |
0.0
|
p0
|
float
|
Center momentum |
0.0
|
sigma
|
float
|
Width of the Gaussian |
1.0
|
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(Q, x, p) where Q is the Husimi function |
Example
Q, x, p = husimi_function_gaussian(x0=0, p0=0, sigma=1.0) plot_husimi(Q, x, p, title="Gaussian State")
Source code in psiqit\visualization\husimi.py
husimi_function_coherent_state(alpha, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Husimi Q-function for a coherent state |α⟩
Q(β) = (1/π) |⟨β|α⟩|² = (1/π) exp(-|β-α|²)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
complex
|
Complex amplitude of the coherent state |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(Q, x, p) where Q is the Husimi function |
Example
alpha = 1.0 + 1j*0.5 Q, x, p = husimi_function_coherent_state(alpha) plot_husimi(Q, x, p, title="Coherent State")
Source code in psiqit\visualization\husimi.py
husimi_function_fock_state(n, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Husimi Q-function for a Fock state |n⟩
Q(α) = (1/π) |⟨α|n⟩|² = (1/π) e^{-|α|²} |α|^{2n} / n!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Fock state number |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(Q, x, p) where Q is the Husimi function |
Example
Q, x, p = husimi_function_fock_state(n=2) plot_husimi(Q, x, p, title="Fock State |2⟩")
Source code in psiqit\visualization\husimi.py
husimi_function_thermal_state(n_bar, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Husimi Q-function for a thermal state
Q(α) = (1/π) (1/(n_bar+1)) exp(-|α|²/(n_bar+1))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_bar
|
float
|
Average photon number |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(Q, x, p) where Q is the Husimi function |
Example
Q, x, p = husimi_function_thermal_state(n_bar=2.0) plot_husimi(Q, x, p, title="Thermal State")
Source code in psiqit\visualization\husimi.py
husimi_function_wavefunction(psi_func, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Husimi Q-function from a wavefunction
Q(x, p) = (1/π) |∫ dy ψ(y) e^{-i p y / ħ} (1/√(2πσ²)) e^{-(x-y)²/(4σ²)}|²
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psi_func
|
callable
|
Wavefunction ψ(x) as a callable |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(Q, x, p) where Q is the Husimi function |
Example
def psi(x): ... return np.exp(-x**2/2) / np.sqrt(np.sqrt(np.pi)) Q, x, p = husimi_function_wavefunction(psi) plot_husimi(Q, x, p)
Source code in psiqit\visualization\husimi.py
husimi_function_density_matrix(rho, x_range=(-5, 5), p_range=(-5, 5), n_points=100, hbar=1.0)
Compute the Husimi Q-function from a density matrix
Q(α) = (1/π) ⟨α|ρ|α⟩
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rho
|
ndarray
|
Density matrix in Fock basis |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
(-5, 5)
|
p_range
|
Tuple[float, float]
|
(p_min, p_max) range |
(-5, 5)
|
n_points
|
int
|
Number of points in each dimension |
100
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[ndarray, ndarray, ndarray]
|
(Q, x, p) where Q is the Husimi function |
Source code in psiqit\visualization\husimi.py
plot_husimi(Q, x, p, title='Husimi Q-Function', cmap='hot', save_path=None, figsize=(8, 6), show_colorbar=True, show_contours=False, contour_levels=10)
Plot the Husimi Q-function in phase space
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Q
|
ndarray
|
Husimi Q-function values |
required |
x
|
ndarray
|
x-axis (position) values |
required |
p
|
ndarray
|
y-axis (momentum) values |
required |
title
|
str
|
Title of the plot |
'Husimi Q-Function'
|
cmap
|
str
|
Colormap name |
'hot'
|
save_path
|
Optional[str]
|
Path to save the figure (if None, displays) |
None
|
figsize
|
Tuple[float, float]
|
Figure size (width, height) |
(8, 6)
|
show_colorbar
|
bool
|
Show colorbar |
True
|
show_contours
|
bool
|
Show contour lines |
False
|
contour_levels
|
int
|
Number of contour levels |
10
|
Example
Q, x, p = husimi_function_coherent_state(1+1j, (-3, 3), (-3, 3)) plot_husimi(Q, x, p, title="Coherent State")
Save to file
plot_husimi(Q, x, p, save_path="husimi.png")
Source code in psiqit\visualization\husimi.py
husimi_negativity(Q)
Compute the negativity of the Husimi Q-function
The Husimi Q-function is always non-negative for valid quantum states. However, for some distributions, it may have negative values due to numerical errors or approximations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Q
|
ndarray
|
Husimi Q-function values |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Negativity (sum of negative values) |
Example
neg = husimi_negativity(Q) print(f"Negativity: {neg:.6f}")
Source code in psiqit\visualization\husimi.py
husimi_entropy(Q, base='e')
Compute the entropy of the Husimi Q-function
S = -∫ Q(α) log(Q(α)) d²α
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Q
|
ndarray
|
Husimi Q-function values |
required |
base
|
str
|
Logarithm base ('e', '2', '10') |
'e'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Entropy |
Example
ent = husimi_entropy(Q) print(f"Entropy: {ent:.6f}")
Source code in psiqit\visualization\husimi.py
husimi_variance(Q, x, p)
Compute the variance of the Husimi Q-function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Q
|
ndarray
|
Husimi Q-function values |
required |
x
|
ndarray
|
x-axis (position) values |
required |
p
|
ndarray
|
y-axis (momentum) values |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[float, float]
|
(var_x, var_p) |
Example
var_x, var_p = husimi_variance(Q, x, p) print(f"Var(x): {var_x:.4f}, Var(p): {var_p:.4f}")
Source code in psiqit\visualization\husimi.py
animate_wavefunction(psi_func, x_range, t_range, n_x=200, n_frames=100, interval=50, save_path=None, figsize=(10, 6), show_real_imag=True, show_probability=True, ylim=None, title='Wavefunction Animation')
Animate a time-dependent wavefunction
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psi_func
|
Callable[[ndarray, float], ndarray]
|
Wavefunction function ψ(x, t) returning complex array |
required |
x_range
|
Tuple[float, float]
|
(x_min, x_max) range |
required |
t_range
|
Tuple[float, float]
|
(t_min, t_max) range |
required |
n_x
|
int
|
Number of spatial points |
200
|
n_frames
|
int
|
Number of animation frames |
100
|
interval
|
int
|
Time between frames in milliseconds |
50
|
save_path
|
Optional[str]
|
Path to save animation (if None, displays) |
None
|
figsize
|
Tuple[float, float]
|
Figure size (width, height) |
(10, 6)
|
show_real_imag
|
bool
|
Show real and imaginary parts |
True
|
show_probability
|
bool
|
Show probability density |
True
|
ylim
|
Optional[Tuple[float, float]]
|
Y-axis limits (if None, auto) |
None
|
title
|
str
|
Title of the animation |
'Wavefunction Animation'
|
Example
import numpy as np def psi_func(x, t): ... # Gaussian wavepacket ... sigma = 1.0 ... k0 = 1.0 ... x0 = 0.0 ... return np.exp(-(x - x0 - t)2/(2*sigma2)) * np.exp(1j(k0x - t)) animate_wavefunction(psi_func, (-5, 5), (0, 10), n_frames=100)
Source code in psiqit\visualization\animation.py
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
animate_bloch_sphere(states, interval=200, save_path=None, figsize=(8, 8), title='Bloch Sphere Animation', color='purple', show_trace=False, trace_length=None)
Animate states on the Bloch sphere
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
List[Union[Ket, ndarray, List, Tuple[float, float, float]]]
|
List of states to animate |
required |
interval
|
int
|
Time between frames in milliseconds |
200
|
save_path
|
Optional[str]
|
Path to save animation (if None, displays) |
None
|
figsize
|
Tuple[float, float]
|
Figure size |
(8, 8)
|
title
|
str
|
Title of the animation |
'Bloch Sphere Animation'
|
color
|
str
|
Color of the state vector |
'purple'
|
show_trace
|
bool
|
Show trace of previous states |
False
|
trace_length
|
Optional[int]
|
Number of previous states to show in trace |
None
|
Example
from psiqit.quantum import zero, plus, minus states = [zero(), plus(), minus(), zero()] animate_bloch_sphere(states, interval=500)
Source code in psiqit\visualization\animation.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
animate_parametric_curve(curve_func, t_range, n_frames=100, interval=50, save_path=None, figsize=(8, 8), title='Parametric Curve on Bloch Sphere', color='purple', show_trace=True)
Animate a parametric curve on the Bloch sphere
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
curve_func
|
Callable[[float], Tuple[float, float, float]]
|
Function f(t) -> (x, y, z) on the Bloch sphere |
required |
t_range
|
Tuple[float, float]
|
(t_min, t_max) range |
required |
n_frames
|
int
|
Number of animation frames |
100
|
interval
|
int
|
Time between frames in milliseconds |
50
|
save_path
|
Optional[str]
|
Path to save animation |
None
|
figsize
|
Tuple[float, float]
|
Figure size |
(8, 8)
|
title
|
str
|
Title of the animation |
'Parametric Curve on Bloch Sphere'
|
color
|
str
|
Color of the curve |
'purple'
|
show_trace
|
bool
|
Show the full trace of the curve |
True
|
Example
def curve(t): ... # Circular path on equator ... return (np.cos(t), np.sin(t), 0.0) animate_parametric_curve(curve, (0, 2*np.pi), n_frames=50)
Source code in psiqit\visualization\animation.py
animate_quantum_circuit(circuit, shots=100, interval=200, save_path=None)
Animate measurement outcomes of a quantum circuit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
QuantumCircuit object |
required | |
shots
|
int
|
Number of measurement shots |
100
|
interval
|
int
|
Time between frames in milliseconds |
200
|
save_path
|
Optional[str]
|
Path to save animation |
None
|
Example
from psiqit.circuits import QuantumCircuit circ = QuantumCircuit(2) circ.h(0).cx(0, 1) animate_quantum_circuit(circ, shots=100)
Source code in psiqit\visualization\animation.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
render_equation(latex, output_file=None, fontsize=16, dpi=150, figsize=(8, 2))
Render a LaTeX equation as an image or return as string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latex
|
str
|
LaTeX equation string |
required |
output_file
|
Optional[str]
|
Path to save the image (if None, returns LaTeX string) |
None
|
fontsize
|
int
|
Font size for the equation |
16
|
dpi
|
int
|
Image resolution |
150
|
figsize
|
Tuple[float, float]
|
Figure size (width, height) |
(8, 2)
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
LaTeX string or path to saved image |
Example
latex = r"E = mc^2" render_equation(latex, output_file="einstein.png")
Or display in notebook
from IPython.display import display, Latex display(Latex(render_equation(latex)))
Source code in psiqit\visualization\symbolic_plot.py
schrodinger_equation(time_dependent=True)
Get the Schrodinger equation in LaTeX format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_dependent
|
bool
|
If True, returns time-dependent Schrodinger equation If False, returns time-independent Schrodinger equation |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
LaTeX equation string |
Example
print(schrodinger_equation()) i\hbar\frac{\partial}{\partial t}\psi = \hat{H}\psi print(schrodinger_equation(time_dependent=False)) \hat{H}\psi = E\psi
Source code in psiqit\visualization\symbolic_plot.py
dirac_notation()
Get common Dirac notation symbols in LaTeX
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, str]
|
Mapping of notation to LaTeX code |
Example
dirac = dirac_notation() print(dirac['ket_zero']) # |0\rangle print(dirac['inner_product']) # \langle \phi | \psi \rangle
Source code in psiqit\visualization\symbolic_plot.py
pauli_matrices()
Get Pauli matrices in LaTeX format
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, str]
|
Pauli matrices as LaTeX strings |
Example
pauli = pauli_matrices() print(pauli['X']) \sigma_x = \begin{pmatrix} 0 & 1 \ 1 & 0 \end{pmatrix}
Source code in psiqit\visualization\symbolic_plot.py
bloch_sphere_equation(theta=None, phi=None)
Get the Bloch sphere equation in LaTeX format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
Optional[float]
|
Polar angle (if provided, includes specific state) |
None
|
phi
|
Optional[float]
|
Azimuthal angle (if provided, includes specific state) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
LaTeX equation string |
Example
print(bloch_sphere_equation()) |\psi\rangle = \cos(\theta/2)|0\rangle + e^{i\phi}\sin(\theta/2)|1\rangle print(bloch_sphere_equation(theta=np.pi/4, phi=np.pi/2)) |\psi\rangle = \cos(\pi/8)|0\rangle + e^{i\pi/2}\sin(\pi/8)|1\rangle
Source code in psiqit\visualization\symbolic_plot.py
heisenberg_uncertainty()
Get the Heisenberg uncertainty principle in LaTeX format
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
LaTeX equation string |
Example
print(heisenberg_uncertainty()) \Delta x \Delta p \geq \frac{\hbar}{2}
Source code in psiqit\visualization\symbolic_plot.py
commutation_relation()
Get the canonical commutation relation in LaTeX format
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
LaTeX equation string |
Example
print(commutation_relation()) [x, p] = i\hbar
Source code in psiqit\visualization\symbolic_plot.py
maxwell_equations()
Get Maxwell's equations in LaTeX format
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: List of LaTeX equation strings |
Example
for eq in maxwell_equations(): ... print(eq) \nabla \cdot \mathbf{E} = \frac{\rho}{\varepsilon_0} \nabla \cdot \mathbf{B} = 0 \nabla \times \mathbf{E} = -\frac{\partial \mathbf{B}}{\partial t} \nabla \times \mathbf{B} = \mu_0 \mathbf{J} + \mu_0 \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t}
Source code in psiqit\visualization\symbolic_plot.py
quantum_equations()
Get common quantum mechanics equations in LaTeX format
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, str]
|
Mapping of equation names to LaTeX strings |
Example
eqs = quantum_equations() print(eqs['schrodinger']) i\hbar\frac{\partial}{\partial t}\psi = \hat{H}\psi
Source code in psiqit\visualization\symbolic_plot.py
render_equation_as_image(latex, output_path, fontsize=20, dpi=200, figsize=(10, 2), bg_color='white', text_color='black')
Render a LaTeX equation as an image file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latex
|
str
|
LaTeX equation string |
required |
output_path
|
str
|
Path to save the image |
required |
fontsize
|
int
|
Font size |
20
|
dpi
|
int
|
Image resolution |
200
|
figsize
|
Tuple[float, float]
|
Figure size (width, height) |
(10, 2)
|
bg_color
|
str
|
Background color |
'white'
|
text_color
|
str
|
Text color |
'black'
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if successful, False otherwise |
Example
success = render_equation_as_image( ... r"E = mc^2", ... "einstein.png", ... fontsize=24 ... )
Source code in psiqit\visualization\symbolic_plot.py
info()
Get information about the visualization module
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Module information |
Source code in psiqit\visualization\__init__.py
test()
Run basic tests for the visualization module
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Test results |