qsharp Package

Packages

estimator
utils

Classes

Pauli

A Q# Pauli operator.

QSharpError

An error returned from the Q# interpreter.

Result

A Q# measurement result.

ShotResult

A single result of a shot.

StateDump

A state dump returned from the Q# interpreter.

TargetProfile

A Q# target profile.

A target profile describes the capabilities of the hardware or simulator which will be used to run the Q# program.

Functions

circuit

Synthesizes a circuit for a Q# program. Either an entry expression or an operation must be provided.

an operation of a lambda expression. The operation must take only qubits or arrays of qubits as parameters.

circuit(entry_expr: str | None = None, *, operation: str | None = None) -> Circuit

Parameters

Name Description
entry_expr
default value: None

Keyword-Only Parameters

Name Description
operation
Required

Exceptions

Type Description

If there is an error synthesizing the circuit.

compile

Compiles the Q# source code into a program that can be submitted to a target.

To get the QIR string from the compiled program, use str().

Example:

compile(entry_expr: str) -> QirInputData

Parameters

Name Description
entry_expr
Required

The Q# expression that will be used as the entrypoint for the program.

Keyword-Only Parameters

Name Description
operation
Required

Returns

Type Description

The compiled program.

Exceptions

Type Description

If there is an error synthesizing the circuit.

dump_circuit

Dumps the current circuit state of the interpreter.

This circuit will contain the gates that have been applied in the simulator up to the current point.

dump_circuit() -> Circuit

Keyword-Only Parameters

Name Description
operation
Required

Exceptions

Type Description

If there is an error synthesizing the circuit.

dump_machine

Returns the sparse state vector of the simulator as a StateDump object.

dump_machine() -> StateDump

Keyword-Only Parameters

Name Description
operation
Required

Returns

Type Description

The state of the simulator.

Exceptions

Type Description

If there is an error synthesizing the circuit.

estimate

Estimates resources for Q# source code.

estimate(entry_expr, params: Dict[str, Any] | List | EstimatorParams | None = None) -> EstimatorResult

Parameters

Name Description
entry_expr
Required

The entry expression.

params

The parameters to configure physical estimation.

default value: None

Keyword-Only Parameters

Name Description
operation
Required

Returns

Type Description

The estimated resources.

Exceptions

Type Description

If there is an error synthesizing the circuit.

eval

Evaluates Q# source code.

Output is printed to console.

eval(source: str) -> Any

Parameters

Name Description
source
Required

The Q# source code to evaluate.

Keyword-Only Parameters

Name Description
operation
Required

Returns

Type Description

The value returned by the last statement in the source code.

Exceptions

Type Description

If there is an error evaluating the source code.

init

Initializes the Q# interpreter.

init(*, target_profile: TargetProfile = TargetProfile.Unrestricted, project_root: str | None = None, language_features: List[str] = []) -> Config

Parameters

Name Description
target_profile
Required

Setting the target profile allows the Q# interpreter to generate programs that are compatible with a specific target. See :py:class: qsharp.TargetProfile.

project_root
Required

An optional path to a root directory with a Q# project to include. It must contain a qsharp.json project manifest.

Keyword-Only Parameters

Name Description
target_profile
default value: TargetProfile.Unrestricted
project_root
Required
language_features
Required

Exceptions

Type Description

If there is an error synthesizing the circuit.

run

Runs the given Q# expression for the given number of shots. Each shot uses an independent instance of the simulator.

a List of ShotResults is returned.

run(entry_expr: str, shots: int, *, on_result: Callable[[ShotResult], None] | None = None, save_events: bool = False) -> List[Any]

Parameters

Name Description
entry_expr
Required
shots
Required

Keyword-Only Parameters

Name Description
on_result
Required
save_events
Required

Exceptions

Type Description

If there is an error interpreting the input.

set_classical_seed

Sets the seed for the random number generator used for standard library classical random number operations. This applies to all Q# code executed, compiled, or estimated.

set_classical_seed(seed: int | None) -> None

Parameters

Name Description
seed
Required

The seed to use for the classical random number generator. If None, the seed will be generated from entropy.

Keyword-Only Parameters

Name Description
operation
Required

Exceptions

Type Description

If there is an error synthesizing the circuit.

set_quantum_seed

Sets the seed for the random number generator used for quantum measurements. This applies to all Q# code executed, compiled, or estimated.

set_quantum_seed(seed: int | None) -> None

Parameters

Name Description
seed
Required

The seed to use for the quantum random number generator. If None, the seed will be generated from entropy.

Keyword-Only Parameters

Name Description
operation
Required

Exceptions

Type Description

If there is an error synthesizing the circuit.