# 量子コンピューティング向け言語Q#の問題集 QuantumKatas Measurements Task 1.10 ベル状態の識別

## 解答1

```    // Task 1.10. Distinguish four Bell states
// Input: two qubits (stored in an array) which are guaranteed to be in one of the four Bell states:
//         |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)
//         |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)
//         |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)
//         |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)
// Output: 0 if qubits were in |Φ⁺⟩ state,
//         1 if they were in |Φ⁻⟩ state,
//         2 if they were in |Ψ⁺⟩ state,
//         3 if they were in |Ψ⁻⟩ state.
// The state of the qubits at the end of the operation does not matter.
operation BellState (qs : Qubit[]) : Int {
CNOT(qs, qs);
H(qs);

let q0 = M(qs);
let q1 = M(qs);

if (q0 == Zero && q1 == Zero) {
return 0;
} elif (q0 == One && q1 == Zero) {
return 1;
} elif (q0 == Zero && q1 == One) {
return 2;
} else {
return 3;
}
}
```

## 解答2

```    // Task 1.10. Distinguish four Bell states
// Input: two qubits (stored in an array) which are guaranteed to be in one of the four Bell states:
//         |Φ⁺⟩ = (|00⟩ + |11⟩) / sqrt(2)
//         |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)
//         |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)
//         |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)
// Output: 0 if qubits were in |Φ⁺⟩ state,
//         1 if they were in |Φ⁻⟩ state,
//         2 if they were in |Ψ⁺⟩ state,
//         3 if they were in |Ψ⁻⟩ state.
// The state of the qubits at the end of the operation does not matter.
operation BellState (qs : Qubit[]) : Int {
CNOT(qs, qs);
Ry(PI() / 2.0, qs);

let q0 = M(qs);
let q1 = M(qs);

if (q0 == Zero && q1 == Zero) {
return 1;
} elif (q0 == One && q1 == Zero) {
return 0;
} elif (q0 == Zero && q1 == One) {
return 3;
} else {
return 2;
}
}
```

`CNOT(qs, qs);``(Controlled X)([qs], qs);`と同じ。