配置

一些设备的重要默认设置,例如您访问量子硬件的用户凭据、射击次数或连续变量模拟器的截止维度,定义在一个名为 config.toml 的配置文件中。

行为

在第一次导入时,PennyLane尝试通过按优先顺序扫描以下三个目录来加载配置文件:

  1. 当前目录

  2. 存储在环境变量 PENNYLANE_CONF 中的路径

  3. 默认用户配置目录:

    • 在Linux上: ~/.config/pennylane

    • 在Windows上: ~C:\Users\USERNAME\AppData\Local\Xanadu\pennylane

    • 在MacOS上: ~/Library/Preferences/pennylane

如果未找到配置文件,日志中将显示警告信息,所有设备参数在加载设备时需要作为关键字参数传递。

加载的配置可以通过 pennylane.default_config 访问,查看加载的配置文件路径,打印配置选项,通过键(即 pennylane.default_config["main.shots"])访问和修改它们,并保存/加载新的配置文件。

例如:

>>> import pennylane as qml
>>> qml.default_config.path
'config.toml'
>>> print(qml.default_config)
{'main': {'shots': 1000},
 'default': {'gaussian': {'hbar': 2}},
 'strawberryfields': {'fock': {'cutoff_dim': 10, 'shots': 1000, 'hbar': 2}}
}

格式

配置文件 config.toml 使用 TOML 标准。请参见以下示例配置,它配置了一些全局选项,以及插件和插件设备特定的选项。

[main]
# Global PennyLane options.
# Affects every loaded plugin if applicable.
shots = 1000

[strawberryfields.global]
# Options for the Strawberry Fields plugin
# For more details, see the PennyLane-SF documentation:
# https://pennylane-sf.readthedocs.io
hbar = 2
shots = 100

    [strawberryfields.fock]
    # Options for the strawberryfields.fock device
    cutoff_dim = 10
    hbar = 2

    [strawberryfields.gaussian]
    # Indentation doesn't matter in TOML files,
    # but helps provide clarity.

[qiskit.global]
# Global options for the Qiskit plugin.
# For more details, see the PennyLane-Qiskit documentation:
# https://pennylaneqiskit.readthedocs.io/en/latest/index.html

backend = "qasm_simulator"

    [qiskit.aer]
    # Default options for Qiskit Aer

    # set the default backend options for the Qiskit Aer device
    # Note that, in TOML, dictionary key-value pairs are defined
    # using '=' rather than ':'.
    backend_options = {"validation_threshold" = 1e-6}

    [qiskit.ibmq]
    # Default options for IBMQ

    # IBM Quantum Experience authentication token
    ibmqx_token = "XXX"

    # hardware backend device
    backend = "ibmq_rome"

    # pass (optional) provider information
    hub = "MYHUB"
    group = "MYGROUP"
    project = "MYPROJECT"

标准的 PennyLane 选项在 [main] 部分提供。这些选项适用于所有加载的设备。 另外,可以通过在 [plugin.global] 下设置选项来按插件进行指定。

例如,在上述配置文件中,Strawberry Fields 设备将默认加载 shots = 100,而不是 shots = 1000。最后,您还可以通过将选项放置在 [plugin.device] 设置下,来指定逐个设备的设置。