psiqit.variational
Auto-generated documentation for the psiqit.variational module.
psiqit.variational
PSIQIT - Python Scientific Quantum Information Toolkit Variational Module
This module provides variational quantum algorithms and methods.
Submodules:
vqe.py - Variational Quantum Eigensolver (VQE) qaoa.py - Quantum Approximate Optimization Algorithm (QAOA) variational_advanced.py - Advanced variational methods (SSVQE, ADAPT-VQE, VQD) variational_methods.py - General variational methods (Rayleigh-Ritz, TDVP, VMC, Hartree-Fock)
VQEResult
dataclass
Result container for VQE optimization
Attributes:
| Name | Type | Description |
|---|---|---|
optimal_params |
Optional[ndarray]
|
Optimal parameters found |
optimal_energy |
float
|
Optimal energy (ground state energy) |
n_iterations |
int
|
Number of iterations |
history |
List[float]
|
History of energies during optimization |
success |
bool
|
Whether optimization was successful |
message |
str
|
Additional message |
final_state |
Optional[Ket]
|
Final state (if available) |
Source code in psiqit\variational\vqe.py
VQE
Variational Quantum Eigensolver (VQE)
Finds the ground state energy of a Hamiltonian by optimizing a parameterized quantum circuit.
Example
from psiqit.variational import VQE
Define Hamiltonian for a 2-qubit system
hamiltonian = {'Z0Z1': 1.0, 'Z0': 0.5, 'Z1': -0.5} vqe = VQE(n_qubits=2, hamiltonian=hamiltonian, n_layers=2) result = vqe.run(n_iterations=100) print(result.optimal_energy)
Source code in psiqit\variational\vqe.py
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 257 258 259 260 261 262 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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
__init__(n_qubits, hamiltonian, n_layers=2, optimizer='gradient', entangler='cnot')
Initialize VQE
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
hamiltonian
|
Dict[str, float]
|
Hamiltonian as Pauli strings {'Z0Z1': 1.0, 'X0': 0.5, ...} |
required |
n_layers
|
int
|
Number of variational layers |
2
|
optimizer
|
str
|
Optimization method ('gradient', 'cobyla', 'nelder_mead') |
'gradient'
|
entangler
|
str
|
Type of entangling gate ('cnot', 'cz', 'swap') |
'cnot'
|
Source code in psiqit\variational\vqe.py
build_circuit(params=None)
Build the VQE circuit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Optional[ndarray]
|
Parameters (if None, uses current params) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
QuantumCircuit |
QuantumCircuit
|
VQE circuit |
Example
circ = vqe.build_circuit() print(circ.draw())
Source code in psiqit\variational\vqe.py
evaluate_energy(params=None)
Evaluate the Hamiltonian expectation value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Optional[ndarray]
|
Parameters (if None, uses current params) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Energy expectation value |
Example
energy = vqe.evaluate_energy() print(energy)
Source code in psiqit\variational\vqe.py
run(n_iterations=100, learning_rate=0.1, verbose=True, initial_params=None)
Run the VQE optimization
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_iterations
|
int
|
Number of iterations |
100
|
learning_rate
|
float
|
Learning rate for gradient descent |
0.1
|
verbose
|
bool
|
Print progress |
True
|
initial_params
|
Optional[ndarray]
|
Initial parameters (if None, uses random) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
VQEResult |
VQEResult
|
Optimization results |
Example
result = vqe.run(n_iterations=100) print(result.optimal_energy)
Source code in psiqit\variational\vqe.py
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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
get_energy_spectrum(n_states=5)
Get the energy spectrum of the Hamiltonian (classical computation)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_states
|
int
|
Number of lowest states to return |
5
|
Returns:
| Type | Description |
|---|---|
List[float]
|
List[float]: Energy eigenvalues |
Example
spectrum = vqe.get_energy_spectrum() print(spectrum[0]) # Ground state energy
Source code in psiqit\variational\vqe.py
get_ground_state()
Get the ground state (after optimization)
Returns:
| Name | Type | Description |
|---|---|---|
Ket |
Ket
|
Ground state |
Example
state = vqe.get_ground_state() print(state)
get_params()
QAOResult
dataclass
Result container for QAOA optimization
Attributes:
| Name | Type | Description |
|---|---|---|
optimal_params |
Optional[ndarray]
|
Optimal parameters (gamma, beta) |
optimal_energy |
float
|
Optimal energy (minimum of cost Hamiltonian) |
n_iterations |
int
|
Number of iterations |
history |
List[float]
|
History of energies during optimization |
success |
bool
|
Whether optimization was successful |
message |
str
|
Additional message |
final_state |
Optional[Ket]
|
Final state (if available) |
optimal_gamma |
Optional[ndarray]
|
Optimal gamma parameters |
optimal_beta |
Optional[ndarray]
|
Optimal beta parameters |
Source code in psiqit\variational\qaoa.py
__post_init__()
Extract gamma and beta from params if available
QAOA
Quantum Approximate Optimization Algorithm (QAOA)
Solves combinatorial optimization problems by alternating between cost and mixer Hamiltonians.
Example
from psiqit.variational import QAOA
MaxCut problem on a triangle graph
edges = [(0, 1), (1, 2), (0, 2)] hamiltonian = maxcut_hamiltonian(edges) qaoa = QAOA(n_qubits=3, hamiltonian=hamiltonian, p=2) result = qaoa.run(n_iterations=100) print(result.optimal_energy)
Source code in psiqit\variational\qaoa.py
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 257 258 259 260 261 262 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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 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 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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | |
__init__(n_qubits, hamiltonian, p=2, optimizer='gradient', mixer='x')
Initialize QAOA
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
hamiltonian
|
Dict[str, float]
|
Cost Hamiltonian as Pauli strings {'Z0Z1': 1.0, 'Z0': 0.5, ...} |
required |
p
|
int
|
Number of QAOA layers (depth) |
2
|
optimizer
|
str
|
Optimization method ('gradient', 'cobyla', 'nelder_mead') |
'gradient'
|
mixer
|
str
|
Mixer Hamiltonian type ('x', 'xy', 'custom') |
'x'
|
Source code in psiqit\variational\qaoa.py
build_circuit(params=None)
Build the QAOA circuit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Optional[ndarray]
|
Parameters [gamma_0, beta_0, gamma_1, beta_1, ...] If None, uses current params |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
QuantumCircuit |
QuantumCircuit
|
QAOA circuit |
Example
circ = qaoa.build_circuit() print(circ.draw())
Source code in psiqit\variational\qaoa.py
evaluate_energy(params=None)
Evaluate the cost Hamiltonian expectation value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Optional[ndarray]
|
Parameters (if None, uses current params) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Energy expectation value |
Example
energy = qaoa.evaluate_energy() print(energy)
Source code in psiqit\variational\qaoa.py
run(n_iterations=100, learning_rate=0.1, verbose=True, initial_params=None)
Run the QAOA optimization
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_iterations
|
int
|
Number of iterations |
100
|
learning_rate
|
float
|
Learning rate for gradient descent |
0.1
|
verbose
|
bool
|
Print progress |
True
|
initial_params
|
Optional[ndarray]
|
Initial parameters (if None, uses random) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
QAOResult |
QAOResult
|
Optimization results |
Example
result = qaoa.run(n_iterations=100) print(result.optimal_energy)
Source code in psiqit\variational\qaoa.py
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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
get_params()
set_params(params)
get_gamma()
MultiStateResult
dataclass
Result container for multi-state variational methods
Attributes:
| Name | Type | Description |
|---|---|---|
energies |
List[float]
|
List of energies for each state |
states |
List[Ket]
|
List of states found |
success |
bool
|
Whether optimization was successful |
iterations |
int
|
Number of iterations |
message |
str
|
Additional message |
Source code in psiqit\variational\variational_advanced.py
get_energy_gaps()
Get energy gaps between consecutive states
SSVQE
Subspace-Search VQE (SSVQE)
Finds multiple excited states simultaneously by using a weighted sum of energies with orthogonalization constraints.
Example
from psiqit.variational import SSVQE hamiltonian = {'Z0Z1': 1.0, 'Z0': 0.5, 'Z1': -0.5} ssvqe = SSVQE(n_qubits=2, hamiltonian=hamiltonian, n_states=3) result = ssvqe.run(n_iterations=100) print(result.energies)
Source code in psiqit\variational\variational_advanced.py
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 257 258 259 260 261 262 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 | |
__init__(n_qubits, hamiltonian, n_states=3, n_layers=2, entangler='cnot')
Initialize SSVQE
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
hamiltonian
|
Dict[str, float]
|
Hamiltonian as Pauli strings |
required |
n_states
|
int
|
Number of states to find |
3
|
n_layers
|
int
|
Number of variational layers |
2
|
entangler
|
str
|
Type of entangling gate |
'cnot'
|
Source code in psiqit\variational\variational_advanced.py
build_circuit(params=None)
Build circuits for all states
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Optional[ndarray]
|
Parameters (if None, uses current params) |
None
|
Returns:
| Type | Description |
|---|---|
List[QuantumCircuit]
|
List[QuantumCircuit]: Circuits for each state |
Source code in psiqit\variational\variational_advanced.py
evaluate_state_energy(state_idx, params=None)
Evaluate energy of a specific state
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_idx
|
int
|
Index of the state |
required |
params
|
Optional[ndarray]
|
Parameters (if None, uses current params) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Energy of the state |
Source code in psiqit\variational\variational_advanced.py
evaluate_cost(params=None)
Evaluate the SSVQE cost function
Cost = Σ_i w_i * E_i where w_i are weights With orthogonalization constraints enforced by the reference states
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Optional[ndarray]
|
Parameters (if None, uses current params) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Cost value |
Source code in psiqit\variational\variational_advanced.py
run(n_iterations=100, learning_rate=0.1, verbose=True, initial_params=None)
Run SSVQE optimization
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_iterations
|
int
|
Number of iterations |
100
|
learning_rate
|
float
|
Learning rate |
0.1
|
verbose
|
bool
|
Print progress |
True
|
initial_params
|
Optional[ndarray]
|
Initial parameters |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
MultiStateResult |
MultiStateResult
|
Optimization results |
Source code in psiqit\variational\variational_advanced.py
ADAPTVQE
ADAPT-VQE (Adaptive Derivative-Assembled Pseudo-Trotter VQE)
Builds the ansatz adaptively by selecting operators from a pool based on their gradients.
Example
from psiqit.variational import ADAPTVQE hamiltonian = {'Z0Z1': 1.0, 'Z0': 0.5, 'Z1': -0.5} adapt = ADAPTVQE(n_qubits=2, hamiltonian=hamiltonian) result = adapt.run(max_iterations=10) print(result.optimal_energy)
Source code in psiqit\variational\variational_advanced.py
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 424 425 426 427 428 429 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 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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | |
__init__(n_qubits, hamiltonian, operator_pool=None)
Initialize ADAPT-VQE
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
hamiltonian
|
Dict[str, float]
|
Hamiltonian as Pauli strings |
required |
operator_pool
|
Optional[List[Operator]]
|
Custom operator pool (if None, creates default) |
None
|
Source code in psiqit\variational\variational_advanced.py
run(max_iterations=10, grad_threshold=0.01, n_iterations=100, learning_rate=0.1, verbose=True)
Run ADAPT-VQE optimization
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_iterations
|
int
|
Maximum number of operator additions |
10
|
grad_threshold
|
float
|
Threshold for adding operators |
0.01
|
n_iterations
|
int
|
Number of iterations per VQE optimization |
100
|
learning_rate
|
float
|
Learning rate for VQE |
0.1
|
verbose
|
bool
|
Print progress |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
VQEResult |
VQEResult
|
Optimization results |
Source code in psiqit\variational\variational_advanced.py
VariationalQuantumDeflation
Variational Quantum Deflation (VQD)
Finds excited states by penalizing overlap with previously found states.
Example
from psiqit.variational import VariationalQuantumDeflation hamiltonian = {'Z0Z1': 1.0, 'Z0': 0.5, 'Z1': -0.5} vqd = VariationalQuantumDeflation(n_qubits=2, hamiltonian=hamiltonian) result = vqd.find_excited_state(penalty_weight=1.0) print(result.optimal_energy)
Source code in psiqit\variational\variational_advanced.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 | |
__init__(n_qubits, hamiltonian, n_layers=2, entangler='cnot')
Initialize VQD
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits |
required |
hamiltonian
|
Dict[str, float]
|
Hamiltonian as Pauli strings |
required |
n_layers
|
int
|
Number of variational layers |
2
|
entangler
|
str
|
Type of entangling gate |
'cnot'
|
Source code in psiqit\variational\variational_advanced.py
find_excited_state(penalty_weight=1.0, n_iterations=100, learning_rate=0.1, verbose=True)
Find the next excited state
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
penalty_weight
|
float
|
Weight of the overlap penalty |
1.0
|
n_iterations
|
int
|
Number of iterations |
100
|
learning_rate
|
float
|
Learning rate |
0.1
|
verbose
|
bool
|
Print progress |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
VQEResult |
VQEResult
|
Optimization results |
Source code in psiqit\variational\variational_advanced.py
VariationalResult
dataclass
Result container for variational methods
Attributes:
| Name | Type | Description |
|---|---|---|
optimal_params |
Optional[ndarray]
|
Optimal parameters found |
optimal_value |
float
|
Optimal value (energy, cost, etc.) |
iterations |
int
|
Number of iterations |
history |
List[float]
|
History of values during optimization |
success |
bool
|
Whether optimization was successful |
method |
str
|
Name of the method used |
message |
str
|
Additional message |
Source code in psiqit\variational\variational_methods.py
RayleighRitz
Rayleigh-Ritz variational method
Finds the minimum of the Rayleigh quotient: R(ψ) = ⟨ψ|H|ψ⟩ / ⟨ψ|ψ⟩
Example
from psiqit.variational import RayleighRitz
Define a matrix (Hamiltonian)
H = np.array([[2, 1], [1, 2]])
Define ansatz: |ψ(θ)⟩ = cos(θ)|0⟩ + sin(θ)|1⟩
def ansatz(theta): ... return np.array([np.cos(theta), np.sin(theta)]) rr = RayleighRitz(H, ansatz, n_params=1) result = rr.optimize(n_iterations=50) print(result.optimal_value)
Source code in psiqit\variational\variational_methods.py
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 | |
__init__(matrix, ansatz, n_params=1)
Initialize Rayleigh-Ritz
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Matrix (Hamiltonian) to diagonalize |
required |
ansatz
|
Callable[[ndarray], ndarray]
|
Ansatz function that takes parameters and returns state vector |
required |
n_params
|
int
|
Number of parameters |
1
|
Source code in psiqit\variational\variational_methods.py
expectation(params)
Compute the Rayleigh quotient
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ndarray
|
Parameters |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Rayleigh quotient value |
Source code in psiqit\variational\variational_methods.py
optimize(n_iterations=100, learning_rate=0.1, verbose=False, initial_params=None)
Optimize the Rayleigh quotient
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_iterations
|
int
|
Number of iterations |
100
|
learning_rate
|
float
|
Learning rate |
0.1
|
verbose
|
bool
|
Print progress |
False
|
initial_params
|
Optional[ndarray]
|
Initial parameters |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
VariationalResult |
VariationalResult
|
Optimization results |
Source code in psiqit\variational\variational_methods.py
TDVP
Time-Dependent Variational Principle (TDVP)
Simulates time evolution of a variational state according to: iħ ∂ψ/∂t = H ψ
Example
from psiqit.variational import TDVP
Define Hamiltonian
H = np.array([[0, 1], [1, 0]])
Define ansatz: |ψ(θ)⟩ = cos(θ)|0⟩ + sin(θ)|1⟩
def ansatz(theta): ... return np.array([np.cos(theta), np.sin(theta)]) tdvp = TDVP(H, ansatz, n_params=1) params0 = np.array([0.0]) params_hist, energy_hist = tdvp.evolve(params0, t_max=10.0, dt=0.01)
Source code in psiqit\variational\variational_methods.py
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 257 258 259 260 261 262 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 414 415 416 417 | |
__init__(hamiltonian, ansatz, n_params, mass=1.0, hbar=1.0)
Initialize TDVP
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hamiltonian
|
ndarray
|
Hamiltonian matrix |
required |
ansatz
|
Callable[[ndarray], ndarray]
|
Ansatz function that takes parameters and returns state vector |
required |
n_params
|
int
|
Number of parameters |
required |
mass
|
float
|
Particle mass |
1.0
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Source code in psiqit\variational\variational_methods.py
evolve(params0, t_max=10.0, dt=0.01, verbose=False)
Evolve the variational state in time
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params0
|
ndarray
|
Initial parameters |
required |
t_max
|
float
|
Maximum time |
10.0
|
dt
|
float
|
Time step |
0.01
|
verbose
|
bool
|
Print progress |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
Tuple[List[List[float]], List[float]]
|
(parameter_history, energy_history) |
Source code in psiqit\variational\variational_methods.py
VariationalMonteCarlo
Variational Monte Carlo (VMC)
Uses Monte Carlo sampling to evaluate and optimize variational wavefunctions.
Example
from psiqit.variational import VariationalMonteCarlo
Define wavefunction: ψ(x, a) = exp(-a*x²)
def wavefunction(x, params): ... return np.exp(-params[0] * x**2)
Define potential: harmonic oscillator V(x) = 0.5*x²
def potential(x): ... return 0.5 * x**2 vmc = VariationalMonteCarlo(wavefunction, potential, n_params=1) result = vmc.optimize(n_samples=1000, n_iterations=50) print(result.optimal_params)
Source code in psiqit\variational\variational_methods.py
424 425 426 427 428 429 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 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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | |
__init__(wavefunction, potential, n_params, kinetic_energy=None, mass=1.0, hbar=1.0)
Initialize Variational Monte Carlo
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wavefunction
|
Callable[[ndarray, ndarray], ndarray]
|
Wavefunction ψ(x, params) |
required |
potential
|
Callable[[ndarray], ndarray]
|
Potential function V(x) |
required |
n_params
|
int
|
Number of parameters |
required |
kinetic_energy
|
Optional[Callable]
|
Kinetic energy function (if None, uses default) |
None
|
mass
|
float
|
Particle mass |
1.0
|
hbar
|
float
|
Reduced Planck constant |
1.0
|
Source code in psiqit\variational\variational_methods.py
optimize(n_samples=5000, n_iterations=50, learning_rate=0.01, verbose=False, initial_params=None)
Optimize the variational parameters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of Monte Carlo samples |
5000
|
n_iterations
|
int
|
Number of optimization iterations |
50
|
learning_rate
|
float
|
Learning rate |
0.01
|
verbose
|
bool
|
Print progress |
False
|
initial_params
|
Optional[ndarray]
|
Initial parameters |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
VariationalResult |
VariationalResult
|
Optimization results |
Source code in psiqit\variational\variational_methods.py
HartreeFock
Hartree-Fock method for electronic structure
Solves the Hartree-Fock equations self-consistently.
Example
from psiqit.variational import HartreeFock
Create Hartree-Fock for 2 electrons in 2 orbitals
hf = HartreeFock(n_electrons=2, n_orbitals=2) result = hf.solve(max_iter=50) print(result.optimal_value) # Total energy
Source code in psiqit\variational\variational_methods.py
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 | |
__init__(n_electrons, n_orbitals, one_electron_integrals=None, two_electron_integrals=None)
Initialize Hartree-Fock
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_electrons
|
int
|
Number of electrons |
required |
n_orbitals
|
int
|
Number of orbitals |
required |
one_electron_integrals
|
Optional[ndarray]
|
h_ij matrix (if None, uses default) |
None
|
two_electron_integrals
|
Optional[ndarray]
|
(ij|kl) tensor (if None, uses default) |
None
|
Source code in psiqit\variational\variational_methods.py
solve(max_iter=50, tol=1e-08, verbose=False)
Solve Hartree-Fock equations self-consistently
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_iter
|
int
|
Maximum number of iterations |
50
|
tol
|
float
|
Convergence tolerance |
1e-08
|
verbose
|
bool
|
Print progress |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
VariationalResult |
VariationalResult
|
Optimization results |
Source code in psiqit\variational\variational_methods.py
maxcut_hamiltonian(edges)
Create MaxCut problem Hamiltonian
H = -Σ_{(i,j)∈E} Z_i Z_j
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
List[Tuple[int, int]]
|
List of edges in the graph |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, float]
|
Hamiltonian as Pauli strings |
Example
edges = [(0, 1), (1, 2), (0, 2)] # Triangle graph H = maxcut_hamiltonian(edges)
H = -Z0Z1 - Z1Z2 - Z0Z2
Source code in psiqit\variational\qaoa.py
qubo_to_hamiltonian(qubo, n_qubits)
Convert QUBO (Quadratic Unconstrained Binary Optimization) to Ising Hamiltonian
QUBO: minimize Σ Q_ij x_i x_j, x_i ∈ {0, 1} Ising: H = Σ J_ij Z_i Z_j + Σ h_i Z_i + constant
Mapping: x_i = (1 - Z_i)/2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qubo
|
Dict[Tuple[int, int], float]
|
QUBO matrix {(i, j): Q_ij} |
required |
n_qubits
|
int
|
Number of qubits |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, float]
|
Hamiltonian as Pauli strings |
Example
Q = {(0, 0): -1.0, (1, 1): -1.0, (0, 1): 2.0} H = qubo_to_hamiltonian(Q, 2)
Source code in psiqit\variational\qaoa.py
maxcut_from_graph(adjacency_matrix)
Create MaxCut Hamiltonian from adjacency matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
ndarray
|
Adjacency matrix of the graph |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, float]
|
Hamiltonian as Pauli strings |
Example
A = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) H = maxcut_from_graph(A)
Source code in psiqit\variational\qaoa.py
info()
Get information about the variational module
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Module information |
Source code in psiqit\variational\__init__.py
test()
Run basic tests for the variational module
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Test results |