Skip to content

Execution

expectation(x, observable, values={}, state=None, backend=BackendName.PYQTORCH, diff_mode=None, noise=None, endianness=Endianness.BIG, configuration=None)

Convenience wrapper for the QuantumModel.expectation method.

PARAMETER DESCRIPTION
x

Circuit, block, or (register+block) to run.

TYPE: Union[QuantumCircuit, AbstractBlock, Register, int]

observable

Observable(s) w.r.t. which the expectation is computed.

TYPE: Union[list[AbstractBlock], AbstractBlock]

values

User-facing parameter dict.

TYPE: dict DEFAULT: {}

state

Initial state.

TYPE: Tensor DEFAULT: None

backend

Name of the backend to run on.

TYPE: BackendName DEFAULT: PYQTORCH

diff_mode

Which differentiation mode to use.

TYPE: Union[DiffMode, str, None] DEFAULT: None

endianness

The target device endianness.

TYPE: Endianness DEFAULT: BIG

configuration

The backend configuration.

TYPE: Union[BackendConfiguration, dict, None] DEFAULT: None

RETURNS DESCRIPTION
Tensor

A wavefunction

from qadence import RX, Z, Register, QuantumCircuit, expectation

reg = Register(1)
block = RX(0, 0.5)
observable = Z(0)
circ = QuantumCircuit(reg, block)

# You can compute the expectation for a
# QuantumCircuit with a given observable.
expectation(circ, observable)

# You can also use only a block.
# In this case the register is constructed automatically to
# Register.line(block.n_qubits)
expectation(block, observable)

# Or a register and block
expectation(reg, block, observable)
Source code in qadence/execution.py
@singledispatch
def expectation(
    x: Union[QuantumCircuit, AbstractBlock, Register, int],
    observable: Union[list[AbstractBlock], AbstractBlock],
    values: dict = {},
    state: Tensor = None,
    backend: BackendName = BackendName.PYQTORCH,
    diff_mode: Union[DiffMode, str, None] = None,
    noise: Union[Noise, None] = None,
    endianness: Endianness = Endianness.BIG,
    configuration: Union[BackendConfiguration, dict, None] = None,
) -> Tensor:
    """Convenience wrapper for the `QuantumModel.expectation` method.

    Arguments:
        x: Circuit, block, or (register+block) to run.
        observable: Observable(s) w.r.t. which the expectation is computed.
        values: User-facing parameter dict.
        state: Initial state.
        backend: Name of the backend to run on.
        diff_mode: Which differentiation mode to use.
        endianness: The target device endianness.
        configuration: The backend configuration.

    Returns:
        A wavefunction

    ```python exec="on" source="material-block"
    from qadence import RX, Z, Register, QuantumCircuit, expectation

    reg = Register(1)
    block = RX(0, 0.5)
    observable = Z(0)
    circ = QuantumCircuit(reg, block)

    # You can compute the expectation for a
    # QuantumCircuit with a given observable.
    expectation(circ, observable)

    # You can also use only a block.
    # In this case the register is constructed automatically to
    # Register.line(block.n_qubits)
    expectation(block, observable)

    # Or a register and block
    expectation(reg, block, observable)
    ```
    """

    raise ValueError(f"Cannot execute {type(x)}")

run(x, *args, values={}, state=None, backend=BackendName.PYQTORCH, endianness=Endianness.BIG, configuration=None)

Convenience wrapper for the QuantumModel.run method.

This is a functools.singledispatched function so it can be called with a number of different arguments. See the examples of the expectation function. This function works exactly the same.

PARAMETER DESCRIPTION
x

Circuit, block, or (register+block) to run.

TYPE: Union[QuantumCircuit, AbstractBlock, Register, int]

values

User-facing parameter dict.

TYPE: dict DEFAULT: {}

state

Initial state.

TYPE: Tensor DEFAULT: None

backend

Name of the backend to run on.

TYPE: BackendName DEFAULT: PYQTORCH

endianness

The target device endianness.

TYPE: Endianness DEFAULT: BIG

configuration

The backend configuration.

TYPE: Union[BackendConfiguration, dict, None] DEFAULT: None

RETURNS DESCRIPTION
Tensor

A wavefunction

Source code in qadence/execution.py
@singledispatch
def run(
    x: Union[QuantumCircuit, AbstractBlock, Register, int],
    *args: Any,
    values: dict = {},
    state: Tensor = None,
    backend: BackendName = BackendName.PYQTORCH,
    endianness: Endianness = Endianness.BIG,
    configuration: Union[BackendConfiguration, dict, None] = None,
) -> Tensor:
    """Convenience wrapper for the `QuantumModel.run` method.

     This is a
    `functools.singledispatch`ed function so it can be called with a number of different arguments.
    See the examples of the [`expectation`][qadence.execution.expectation] function. This function
    works exactly the same.

    Arguments:
        x: Circuit, block, or (register+block) to run.
        values: User-facing parameter dict.
        state: Initial state.
        backend: Name of the backend to run on.
        endianness: The target device endianness.
        configuration: The backend configuration.

    Returns:
        A wavefunction
    """
    raise ValueError(f"Cannot run {type(x)}")

sample(x, *args, values={}, state=None, n_shots=100, backend=BackendName.PYQTORCH, endianness=Endianness.BIG, noise=None, configuration=None)

Convenience wrapper for the QuantumModel.sample method.

PARAMETER DESCRIPTION
x

Circuit, block, or (register+block) to run.

TYPE: Union[QuantumCircuit, AbstractBlock, Register, int]

values

User-facing parameter dict.

TYPE: dict DEFAULT: {}

state

Initial state.

TYPE: Union[Tensor, None] DEFAULT: None

n_shots

Number of shots per element in the batch.

TYPE: int DEFAULT: 100

backend

Name of the backend to run on.

TYPE: BackendName DEFAULT: PYQTORCH

endianness

The target device endianness.

TYPE: Endianness DEFAULT: BIG

noise

The noise model to use if any.

TYPE: Union[Noise, None] DEFAULT: None

configuration

The backend configuration.

TYPE: Union[BackendConfiguration, dict, None] DEFAULT: None

RETURNS DESCRIPTION
list[Counter]

A list of Counter instances with the sample results

Source code in qadence/execution.py
@singledispatch
def sample(
    x: Union[QuantumCircuit, AbstractBlock, Register, int],
    *args: Any,
    values: dict = {},
    state: Union[Tensor, None] = None,
    n_shots: int = 100,
    backend: BackendName = BackendName.PYQTORCH,
    endianness: Endianness = Endianness.BIG,
    noise: Union[Noise, None] = None,
    configuration: Union[BackendConfiguration, dict, None] = None,
) -> list[Counter]:
    """Convenience wrapper for the `QuantumModel.sample` method.

    Arguments:
        x: Circuit, block, or (register+block) to run.
        values: User-facing parameter dict.
        state: Initial state.
        n_shots: Number of shots per element in the batch.
        backend: Name of the backend to run on.
        endianness: The target device endianness.
        noise: The noise model to use if any.
        configuration: The backend configuration.

    Returns:
        A list of Counter instances with the sample results
    """
    raise ValueError(f"Cannot sample from {type(x)}")