QuantumState
An extended class of Statevector from Qiskit.
This class extends the Statevector class from Qiskit to provide additional functionalities specific to quantum state manipulations and measurements.
Attributes:
Name | Type | Description |
---|---|---|
_num_of_qubit |
int
|
The number of qubits in the quantum state. |
See Also
Source code in QIRT/quantum_state.py
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 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 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 |
|
data: NDArray[np.complex128]
property
Get the data of the quantum state vector.
This property returns the data of the quantum state vector stored in the object.
Returns:
Type | Description |
---|---|
NDArray[complex128]
|
NDArray[np.complex128]: The data of the quantum state vector. |
num_of_qubit: int
property
Get the number of qubits in the quantum state.
This property returns the total number of qubits that are currently represented in the quantum state vector.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of qubits in the quantum state. |
__init__(data, dims=None)
Initialize a QuantumState object.
This constructor initializes the QuantumState object by calling the constructor of the base Statevector class from Qiskit. It also calculates and stores the number of qubits in the quantum state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
array or list or Statevector or Operator or QuantumCircuit or Instruction
|
Data from which the statevector can be constructed. This can be either a complex
vector, another statevector, a |
required |
dims
|
int or tuple or list
|
Optional. The subsystem dimension of the state (See additional information). |
None
|
Source code in QIRT/quantum_state.py
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 |
|
apply(other, qargs=None)
Apply a quantum circuit to the quantum state.
This method applies the given operator to the quantum state, evolving it according to the operator's effect.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
QuantumCircuit
|
The operator used to evolve the quantum state. |
required |
qargs
|
list[int] | None
|
A list of subsystem positions of the QuantumState to apply the operator on. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
QuantumState |
QuantumState
|
The quantum state after evolution. |
Raises:
Type | Description |
---|---|
QiskitError
|
If the operator dimension does not match the specified quantum state subsystem dimensions. |
Source code in QIRT/quantum_state.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
draw(output='latex', target_basis=None, show_qubit_index=True, output_length=2, source=False)
Visualize the statevector.
This method provides different visualization options for the quantum state vector, such as LaTeX, matrix/vector form, or other specified formats.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
str
|
Visualization method. Defaults to "latex". Options include:
- "matrix" or "vector": Outputs the QuantumState as a LaTeX formatted matrix.
- "latex": Outputs the QuantumState as a LaTeX formatted expression.
- "repr": ASCII TextMatrix of the QuantumState's |
'latex'
|
target_basis
|
List[str] | str | None
|
The target basis for visualization. Defaults to None. |
None
|
show_qubit_index
|
bool
|
Whether to show qubit indices in the visualization. Defaults to True. |
True
|
output_length
|
int
|
The number of terms in each line, defined as 2^output_length. Defaults to 2 (i.e., 4 terms per line). |
2
|
source
|
bool
|
Whether to return the latex source code for the visualization option "matrix" and "latex". Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Figure | str | TextMatrix | Latex | Latex
|
The visualization output depending on the chosen method. |
Source code in QIRT/quantum_state.py
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 |
|
draw_measurement(measure_bit, target_basis=[], show_qubit_index=True, output_length=2, source=False)
Visualize the measurement results of the quantum state.
This method performs a measurement on specified qubits and visualizes the resulting quantum states and their measurement outcomes in a specified format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
measure_bit
|
List[int] | str
|
The bits (qubits) to measure. Can be a list of indices or a string specifying the bits. |
required |
target_basis
|
List[str] | str
|
The basis in which to perform the measurement. Defaults to basis with minimum entropy. |
[]
|
show_qubit_index
|
bool
|
Whether to show qubit indices in the visualization. Defaults to True. |
True
|
output_length
|
int
|
The number of terms in each line, defined as 2^output_length. Defaults to 2 (i.e., 4 terms per line). |
2
|
source
|
bool
|
Whether to return the source code for the visualization. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
str | Latex
|
The visualization of the measurement results, either as an image or a string representing the source code. |
Raises:
Type | Description |
---|---|
QiskitError
|
If the measurement basis or bit specifications are invalid. |
Source code in QIRT/quantum_state.py
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 |
|
entropy()
Calculate and return the Shannon entropy of the quantum state.
The Shannon entropy is a measure of the quantum state's uncertainty or mixedness.
Returns:
Type | Description |
---|---|
float64
|
np.float64: The Shannon entropy of the quantum state, calculated in base 2. |
Source code in QIRT/quantum_state.py
180 181 182 183 184 185 186 187 188 189 190 191 |
|
from_label(*args)
classmethod
Create a state vector from input coefficients and label strings.
Examples:
>>> QuantumState.from_label("0", "1")
(|0> + |1>)/√2 QuantumState object.
>>> QuantumState.from_label("00", "01", "10", "11")
(|00> + |01> + |10> + |11>)/2 = |++> QuantumState object.
>>> QuantumState.from_label("+", (-1, "-"))
(|+> - |->)/√2 QuantumState object.
>>> QuantumState.from_label((2**0.5, "0"), "+", (-1, "-"))
(√2|0> + |+> - |->)/2 = |+> QuantumState object.
>>> QuantumState.from_label("0", (1j, "1"))
(|0> + i|1>)/√2 = |i> QuantumState object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
str | Tuple[complex, str]
|
Input label strings or tuples of coefficients and label strings. |
()
|
Returns:
Name | Type | Description |
---|---|---|
QuantumState |
QuantumState
|
The state vector object. |
Raises:
Type | Description |
---|---|
QiskitError
|
If labels contain invalid characters or if labels have different numbers of qubits. |
Source code in QIRT/quantum_state.py
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 |
|
state_after_measurement(measure_bit, target_basis=[])
Simulate quantum measurement and return resulting states.
This method simulates the measurement of specified qubits in a given basis and returns the possible post-measurement states of the system.
Examples:
>>> state = QuantumState.from_label("000", "111")
Measure qubit 0 in Z-basis:
>>> z_states = state.state_after_measurement(measure_bit=[0], target_basis="z--")
>>> z_states[0].draw()
|000>
>>> z_states[1].draw()
|111>
Measure qubit 0 in X-basis:
>>> x_states = state.state_after_measurement(measure_bit=[0], target_basis="x--")
>>> x_states[0].draw()
1/√2(|+++> + |+-->)
>>> x_states[1].draw()
1/√2(|-+-> - |--+>)
Measure qubit 0 in Y-basis:
>>> y_states = state.state_after_measurement(measure_bit=[0], target_basis="y--")
>>> y_states[0].draw()
1/√2(|i00> - i|i11>)
>>> y_states[1].draw()
1/√2(|j00> + i|j11>)
Measure qubit 2 in Y-basis:
>>> y_states = state.state_after_measurement(measure_bit=[2], target_basis="--y")
>>> y_states[0].draw()
1/√2(|00i> - i|11i>)
>>> y_states[1].draw()
1/√2(|00j> + i|11j>)
Measure qubits 1 and 2 in X-basis:
>>> x_states = state.state_after_measurement(measure_bit=[1, 2], target_basis="-xx")
>>> x_states[0b00].draw()
|+++>
>>> x_states[0b01].draw()
|-+->
>>> x_states[0b10].draw()
|--+>
>>> x_states[0b11].draw()
|+-->
Understanding and Using the Results
-
List Structure: The returned list contains QuantumState objects, each representing a possible post-measurement state. The number of states in the list depends on the number of measured qubits.
-
Indexing:
- For a single qubit measurement (in any basis: Z, X, or Y):
- states[0]: State corresponding to the measurement result '0'
- states[1]: State corresponding to the measurement result '1'
-
For multi-qubit measurements: The index corresponds to the binary representation of the measurement outcome. E.g., for a two-qubit measurement:
- states[0b00]: Outcome '00'
- states[0b01]: Outcome '01'
- states[0b10]: Outcome '10'
- states[0b11]: Outcome '11'
-
Basis-Specific Interpretations:
- Z-basis: '0' represents |0>, '1' represents |1>
- X-basis: '0' represents |+>, '1' represents |->
- Y-basis: '0' represents |+i>, '1' represents |-i>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
measure_bit
|
List[int] | str
|
Indices of qubits to be measured. Can be a list of integers or a string of qubit indices (e.g., "01" for qubits 0 and 1). |
required |
target_basis
|
List[str] | str
|
Measurement basis for each measured qubit. Supported bases are "x", "y", "z". If not specified, Z-basis is used by default. Can be a list of strings or a string (e.g., ["x", "z"] or "xz"). |
[]
|
Returns:
Type | Description |
---|---|
list[QuantumState]
|
List[QuantumState]: A list of possible post-measurement quantum states. Each state |
list[QuantumState]
|
represents a possible outcome of the measurement process. |
Source code in QIRT/quantum_state.py
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 |
|
tensor(other)
Return the tensor product state self ⊗ other.
This method calculates the tensor product of the quantum states stored in the object and another given quantum state, returning the resulting quantum state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
QuantumState
|
The other quantum state to tensor with. |
required |
Returns:
Name | Type | Description |
---|---|---|
QuantumState |
QuantumState
|
the tensor product operator self ⊗ other. |
Source code in QIRT/quantum_state.py
166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
to_matrix()
Convert the quantum state vector to a column matrix representation.
This method takes the quantum state vector stored in the object and converts it into a column matrix form, which can be useful for various matrix-based operations and calculations.
Returns:
Type | Description |
---|---|
NDArray[complex128]
|
NDArray[np.complex128]: The quantum state represented as a column matrix. |
Source code in QIRT/quantum_state.py
217 218 219 220 221 222 223 224 225 226 227 228 229 |
|