|
15 | 15 | This module contains the :class:`~.LightningQubit` class, a PennyLane simulator device that
|
16 | 16 | interfaces with C++ for fast linear algebra calculations.
|
17 | 17 | """
|
| 18 | +from typing import List |
18 | 19 | from warnings import warn
|
19 | 20 |
|
20 | 21 | import numpy as np
|
|
29 | 30 | import pennylane as qml
|
30 | 31 | from pennylane.devices import DefaultQubit
|
31 | 32 | from pennylane.operation import Expectation
|
| 33 | +from pennylane.wires import Wires |
32 | 34 |
|
33 | 35 | from ._version import __version__
|
34 | 36 |
|
35 | 37 | try:
|
36 | 38 | from .lightning_qubit_ops import (
|
| 39 | + MeasuresC64, |
37 | 40 | StateVectorC64,
|
38 | 41 | AdjointJacobianC64,
|
39 | 42 | VectorJacobianProductC64,
|
| 43 | + MeasuresC128, |
40 | 44 | StateVectorC128,
|
41 | 45 | AdjointJacobianC128,
|
42 | 46 | VectorJacobianProductC128,
|
@@ -493,6 +497,157 @@ def batch_vjp(
|
493 | 497 |
|
494 | 498 | return jacs, vjps
|
495 | 499 |
|
| 500 | + def probability(self, wires=None, shot_range=None, bin_size=None): |
| 501 | + """Return the probability of each computational basis state. |
| 502 | +
|
| 503 | + Devices that require a finite number of shots always return the |
| 504 | + estimated probability. |
| 505 | +
|
| 506 | + Args: |
| 507 | + wires (Iterable[Number, str], Number, str, Wires): wires to return |
| 508 | + marginal probabilities for. Wires not provided are traced out of the system. |
| 509 | + shot_range (tuple[int]): 2-tuple of integers specifying the range of samples |
| 510 | + to use. If not specified, all samples are used. |
| 511 | + bin_size (int): Divides the shot range into bins of size ``bin_size``, and |
| 512 | + returns the measurement statistic separately over each bin. If not |
| 513 | + provided, the entire shot range is treated as a single bin. |
| 514 | +
|
| 515 | + Returns: |
| 516 | + array[float]: list of the probabilities |
| 517 | + """ |
| 518 | + if self.shots is not None: |
| 519 | + return self.estimate_probability(wires=wires, shot_range=shot_range, bin_size=bin_size) |
| 520 | + |
| 521 | + wires = wires or self.wires |
| 522 | + wires = Wires(wires) |
| 523 | + |
| 524 | + # translate to wire labels used by device |
| 525 | + device_wires = self.map_wires(wires) |
| 526 | + |
| 527 | + # To support np.complex64 based on the type of self._state |
| 528 | + dtype = self._state.dtype |
| 529 | + if dtype == np.complex64: |
| 530 | + use_csingle = True |
| 531 | + elif dtype == np.complex128: |
| 532 | + use_csingle = False |
| 533 | + else: |
| 534 | + raise TypeError(f"Unsupported complex Type: {dtype}") |
| 535 | + |
| 536 | + # Initialization of state |
| 537 | + ket = np.ravel(self._state) |
| 538 | + |
| 539 | + if use_csingle: |
| 540 | + ket = ket.astype(np.complex64) |
| 541 | + |
| 542 | + state_vector = StateVectorC64(ket) if use_csingle else StateVectorC128(ket) |
| 543 | + M = MeasuresC64(state_vector) if use_csingle else MeasuresC128(state_vector) |
| 544 | + |
| 545 | + return M.probs(device_wires) |
| 546 | + |
| 547 | + def expval(self, observable, shot_range=None, bin_size=None): |
| 548 | + """Expectation value of the supplied observable. |
| 549 | +
|
| 550 | + Args: |
| 551 | + observable: A PennyLane observable. |
| 552 | + shot_range (tuple[int]): 2-tuple of integers specifying the range of samples |
| 553 | + to use. If not specified, all samples are used. |
| 554 | + bin_size (int): Divides the shot range into bins of size ``bin_size``, and |
| 555 | + returns the measurement statistic separately over each bin. If not |
| 556 | + provided, the entire shot range is treated as a single bin. |
| 557 | +
|
| 558 | + Returns: |
| 559 | + Expectation value of the observable |
| 560 | + """ |
| 561 | + if isinstance(observable.name, List) or observable.name in [ |
| 562 | + "Identity", |
| 563 | + "Projector", |
| 564 | + "Hermitian", |
| 565 | + "Hamiltonian", |
| 566 | + "SparseHamiltonian", |
| 567 | + ]: |
| 568 | + # TODO: requires backend support |
| 569 | + return super().expval(observable, shot_range=shot_range, bin_size=bin_size) |
| 570 | + |
| 571 | + if self.shots is not None: |
| 572 | + # estimate the expectation value |
| 573 | + # TODO: Lightning support for sampling |
| 574 | + samples = self.sample(observable, shot_range=shot_range, bin_size=bin_size) |
| 575 | + return np.squeeze(np.mean(samples, axis=0)) |
| 576 | + |
| 577 | + # To support np.complex64 based on the type of self._state |
| 578 | + dtype = self._state.dtype |
| 579 | + if dtype == np.complex64: |
| 580 | + use_csingle = True |
| 581 | + elif dtype == np.complex128: |
| 582 | + use_csingle = False |
| 583 | + else: |
| 584 | + raise TypeError(f"Unsupported complex Type: {dtype}") |
| 585 | + |
| 586 | + # Initialization of state |
| 587 | + ket = np.ravel(self._pre_rotated_state) |
| 588 | + |
| 589 | + if use_csingle: |
| 590 | + ket = ket.astype(np.complex64) |
| 591 | + |
| 592 | + state_vector = StateVectorC64(ket) if use_csingle else StateVectorC128(ket) |
| 593 | + M = MeasuresC64(state_vector) if use_csingle else MeasuresC128(state_vector) |
| 594 | + |
| 595 | + # translate to wire labels used by device |
| 596 | + observable_wires = self.map_wires(observable.wires) |
| 597 | + |
| 598 | + return M.expval(observable.name, observable_wires) |
| 599 | + |
| 600 | + def var(self, observable, shot_range=None, bin_size=None): |
| 601 | + """Variance of the supplied observable. |
| 602 | +
|
| 603 | + Args: |
| 604 | + observable: A PennyLane observable. |
| 605 | + shot_range (tuple[int]): 2-tuple of integers specifying the range of samples |
| 606 | + to use. If not specified, all samples are used. |
| 607 | + bin_size (int): Divides the shot range into bins of size ``bin_size``, and |
| 608 | + returns the measurement statistic separately over each bin. If not |
| 609 | + provided, the entire shot range is treated as a single bin. |
| 610 | +
|
| 611 | + Returns: |
| 612 | + Variance of the observable |
| 613 | + """ |
| 614 | + if isinstance(observable.name, List) or observable.name in [ |
| 615 | + "Identity", |
| 616 | + "Projector", |
| 617 | + "Hermitian", |
| 618 | + ]: |
| 619 | + # TODO: requires backend support |
| 620 | + return super().var(observable, shot_range=shot_range, bin_size=bin_size) |
| 621 | + |
| 622 | + if self.shots is not None: |
| 623 | + # estimate the var |
| 624 | + # TODO: Lightning support for sampling |
| 625 | + samples = self.sample(observable, shot_range=shot_range, bin_size=bin_size) |
| 626 | + return np.squeeze(np.var(samples, axis=0)) |
| 627 | + |
| 628 | + # To support np.complex64 based on the type of self._state |
| 629 | + dtype = self._state.dtype |
| 630 | + if dtype == np.complex64: |
| 631 | + use_csingle = True |
| 632 | + elif dtype == np.complex128: |
| 633 | + use_csingle = False |
| 634 | + else: |
| 635 | + raise TypeError(f"Unsupported complex Type: {dtype}") |
| 636 | + |
| 637 | + # Initialization of state |
| 638 | + ket = np.ravel(self._pre_rotated_state) |
| 639 | + |
| 640 | + if use_csingle: |
| 641 | + ket = ket.astype(np.complex64) |
| 642 | + |
| 643 | + state_vector = StateVectorC64(ket) if use_csingle else StateVectorC128(ket) |
| 644 | + M = MeasuresC64(state_vector) if use_csingle else MeasuresC128(state_vector) |
| 645 | + |
| 646 | + # translate to wire labels used by device |
| 647 | + observable_wires = self.map_wires(observable.wires) |
| 648 | + |
| 649 | + return M.var(observable.name, observable_wires) |
| 650 | + |
496 | 651 |
|
497 | 652 | if not CPP_BINARY_AVAILABLE:
|
498 | 653 |
|
|
0 commit comments