Skip to content
Pasqal Documentation

Execution

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

Section titled “ expectation(x, observable, values=None, 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: Union[dict, None] DEFAULT: None

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
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@singledispatch
def expectation(
x: Union[QuantumCircuit, AbstractBlock, Register, int],
observable: Union[list[AbstractBlock], AbstractBlock],
values: Union[dict, None] = None,
state: Tensor = None,
backend: BackendName = BackendName.PYQTORCH,
diff_mode: Union[DiffMode, str, None] = None,
noise: Union[NoiseHandler, 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=None, state=None, backend=BackendName.PYQTORCH, endianness=Endianness.BIG, configuration=None)

Section titled “ run(x, *args, values=None, 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: Union[dict, None] DEFAULT: None

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62@singledispatch
def run(
x: Union[QuantumCircuit, AbstractBlock, Register, int],
*args: Any,
values: Union[dict, None] = None,
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=None, state=None, n_shots=100, backend=BackendName.PYQTORCH, endianness=Endianness.BIG, noise=None, configuration=None)

Section titled “ sample(x, *args, values=None, 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: Union[dict, None] DEFAULT: None

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[NoiseHandler, 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
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@singledispatch
def sample(
x: Union[QuantumCircuit, AbstractBlock, Register, int],
*args: Any,
values: Union[dict, None] = None,
state: Union[Tensor, None] = None,
n_shots: int = 100,
backend: BackendName = BackendName.PYQTORCH,
endianness: Endianness = Endianness.BIG,
noise: Union[NoiseHandler, 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)}")