

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 故障診斷 OpenQASM
<a name="braket-troubleshooting-openqasm"></a>

本節提供使用 OpenQASM 3.0 遇到錯誤時可能有用的疑難排解指標。

**Topics**
+ [包含陳述式錯誤](#braket-troubleshooting-openqasm-include-statement-error)
+ [非連續qubits錯誤](#braket-troubleshooting-openqasm-non-contiguous-qubits-error)
+ [混合實體qubits與虛擬qubits錯誤](#braket-troubleshooting-openqasm-mixing-physical-and-virtual-qubits-error)
+ [在相同的程式錯誤qubits中請求結果類型和測量](#braket-troubleshooting-openqasm-result-types-measuring-qubits-error)
+ [超過傳統和qubit註冊限制的錯誤](#braket-troubleshooting-openqasm-register-limits-error)
+ [前面沒有逐字 pragma 錯誤的方塊](#braket-troubleshooting-openqasm-box-not-preceded-by-pragma-error)
+ [逐字方塊缺少原生閘道錯誤](#braket-troubleshooting-openqasm-verbatim-box-missing-native-gates-error)
+ [逐字方塊缺少實體qubits錯誤](#braket-troubleshooting-openqasm-verbatim-box-missing-physical-qubits-error)
+ [逐字 pragma 缺少 "braket" 錯誤](#braket-troubleshooting-openqasm-pragma-naming-error)
+ [單一 qubits無法編製索引錯誤](#braket-troubleshooting-openqasm-qubit-index-error)
+ [兩個qubit閘道qubits中的實體未連線錯誤](#braket-troubleshooting-openqasm-disconnected-physical-qubits-error)
+ [本機模擬器支援警告](#braket-troubleshooting-openqasm-local-simulator-support-warning)

## 包含陳述式錯誤
<a name="braket-troubleshooting-openqasm-include-statement-error"></a>

Braket 目前沒有要包含在 OpenQASM 程式中的標準閘道程式庫檔案。例如，下列範例會引發剖析器錯誤。

```
OPENQASM 3;
include "standardlib.inc";
```

此程式碼會產生錯誤訊息： `No terminal matches '"' in the current parser context, at line 2 col 17.`

## 非連續qubits錯誤
<a name="braket-troubleshooting-openqasm-non-contiguous-qubits-error"></a>

qubits 在裝置功能`true`中`requiresContiguousQubitIndices`設定為 的裝置上使用不連續的 會導致錯誤。

在模擬器和 上執行量子任務時IonQ，下列程式會觸發錯誤。

```
OPENQASM 3;

qubit[4] q;

h q[0];
cnot q[0], q[2];
cnot q[0], q[3];
```

此程式碼會產生錯誤訊息： `Device requires contiguous qubits. Qubit register q has unused qubits q[1], q[4].`

## 混合實體qubits與虛擬qubits錯誤
<a name="braket-troubleshooting-openqasm-mixing-physical-and-virtual-qubits-error"></a>

不允許在相同程式qubits中混合實體qubits與虛擬 ，並導致錯誤。下列程式碼會產生錯誤。

```
OPENQASM 3;

qubit[2] q;
cnot q[0], $1;
```

此程式碼會產生錯誤訊息： `[line 4] mixes physical qubits and qubits registers.`

## 在相同的程式錯誤qubits中請求結果類型和測量
<a name="braket-troubleshooting-openqasm-result-types-measuring-qubits-error"></a>

請求在相同程式中qubits明確測量的結果類型和 會導致錯誤。下列程式碼會產生錯誤。

```
OPENQASM 3;

qubit[2] q;

h q[0];
cnot q[0], q[1];
measure q;

#pragma braket result expectation x(q[0]) @ z(q[1])
```

此程式碼會產生錯誤訊息： `Qubits should not be explicitly measured when result types are requested.`

## 超過傳統和qubit註冊限制的錯誤
<a name="braket-troubleshooting-openqasm-register-limits-error"></a>

只允許一個傳統註冊和一個qubit註冊。下列程式碼會產生錯誤。

```
OPENQASM 3;

qubit[2] q0;
qubit[2] q1;
```

此程式碼會產生錯誤訊息： `[line 4] cannot declare a qubit register. Only 1 qubit register is supported.`

## 前面沒有逐字 pragma 錯誤的方塊
<a name="braket-troubleshooting-openqasm-box-not-preceded-by-pragma-error"></a>

所有方塊前面都必須加上逐字法。下列程式碼會產生錯誤。

```
box{
rx(0.5) $0;
}
```

此程式碼會產生錯誤訊息： `In verbatim boxes, native gates are required. x is not a device native gate.`

## 逐字方塊缺少原生閘道錯誤
<a name="braket-troubleshooting-openqasm-verbatim-box-missing-native-gates-error"></a>

逐字方塊應具有原生閘道和實體 qubits。下列程式碼會產生原生閘道錯誤。

```
#pragma braket verbatim
box{
x $0;
}
```

此程式碼會產生錯誤訊息： `In verbatim boxes, native gates are required. x is not a device native gate.`

## 逐字方塊缺少實體qubits錯誤
<a name="braket-troubleshooting-openqasm-verbatim-box-missing-physical-qubits-error"></a>

逐字方塊必須具有實體 qubits。下列程式碼會產生缺少的實體qubits錯誤。

```
qubit[2] q;

#pragma braket verbatim
box{
rx(0.1) q[0];
}
```

此程式碼會產生錯誤訊息： `Physical qubits are required in verbatim box.`

## 逐字 pragma 缺少 "braket" 錯誤
<a name="braket-troubleshooting-openqasm-pragma-naming-error"></a>

您必須在逐字法中包含「括號」。下列程式碼會產生錯誤。

```
#pragma braket verbatim           // Correct
#pragma verbatim                  // wrong
```

此程式碼會產生錯誤訊息： `You must include “braket” in the verbatim pragma`

## 單一 qubits無法編製索引錯誤
<a name="braket-troubleshooting-openqasm-qubit-index-error"></a>

單一 qubits無法編製索引。下列程式碼會產生錯誤。

```
OPENQASM 3;

qubit q;
h q[0];
```

此程式碼會產生錯誤： `[line 4] single qubit cannot be indexed.`

不過，單一qubit陣列的索引如下所示：

```
OPENQASM 3;

qubit[1] q;
h q[0];   // This is valid
```

## 兩個qubit閘道qubits中的實體未連線錯誤
<a name="braket-troubleshooting-openqasm-disconnected-physical-qubits-error"></a>

若要使用實體 qubits，請先檢查 qubits 以確認裝置使用實體，`device.properties.action[DeviceActionType.OPENQASM].supportPhysicalQubits`然後檢查 `device.properties.paradigm.connectivity.connectivityGraph`或 來驗證連線圖形`device.properties.paradigm.connectivity.fullyConnected`。

```
OPENQASM 3;

cnot $0, $14;
```

此程式碼會產生錯誤訊息： `[line 3] has disconnected qubits 0 and 14`

## 本機模擬器支援警告
<a name="braket-troubleshooting-openqasm-local-simulator-support-warning"></a>

`LocalSimulator` 支援 OpenQASM 中的進階功能，這些功能可能無法在 QPUs 或隨需模擬器上使用。如果您的程式包含 特有的語言功能`LocalSimulator`，如下列範例所示，您將會收到警告。

```
qasm_string = """
qubit[2] q;

h q[0];
ctrl @ x q[0], q[1];
"""
qasm_program = Program(source=qasm_string)
```

此程式碼會產生警告：`此程式只使用 LocalSimulator 中支援的 OpenQASM 語言功能。QPUs 或隨需模擬器可能不支援其中一些功能。

如需支援的 OpenQASM 功能的詳細資訊，請探索[本機模擬器上 OpenQASM 的進階功能支援](braket-openqasm-supported-features.md#braket-openqasm-supported-features-advanced-feature-local-simulator)頁面。