Skip to content

Commit

Permalink
fixed circuit_from_scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
yiiyama committed Mar 30, 2023
1 parent 84e0c59 commit ec9b3fa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
12 changes: 7 additions & 5 deletions source/ja/circuit_from_scratch.md
Expand Up @@ -82,6 +82,8 @@ circuit.draw('mpl')

```{code-cell} ipython3
def get_statevector_array(circuit):
# 渡された回路のコピーを使う
circuit = circuit.copy()
# 量子回路の終状態の状態ベクトルを保存するインストラクション
circuit.save_statevector()
# 再び「おまじない」のtranspileをしてから、run()に渡す
Expand Down Expand Up @@ -857,11 +859,11 @@ print(np.square(np.abs(np.sum(sv_psi.conjugate() * sv_phi))))
それに対し上の回路を1000000回実行した時の$P_0 - P_1$は

```{code-cell} ipython3
qasm_simulator = Aer.get_backend('qasm_simulator')
simulator = AerSimulator()
shots = 1000000
circuit = transpile(circuit, backend=qasm_simulator)
counts = qasm_simulator.run(circuit, shots=shots).result().get_counts()
circuit = transpile(circuit, backend=simulator)
counts = simulator.run(circuit, shots=shots).result().get_counts()
print((counts['0'] - counts['1']) / shots)
```
Expand Down Expand Up @@ -969,8 +971,8 @@ $$
```{code-cell} ipython3
shots = 1000000
circuit = transpile(circuit, backend=qasm_simulator)
counts = qasm_simulator.run(circuit, shots=shots).result().get_counts()
circuit = transpile(circuit, backend=simulator)
counts = simulator.run(circuit, shots=shots).result().get_counts()
print(counts['000'] / shots)
```
Expand Down
3 changes: 2 additions & 1 deletion source/qc_workbook/show_state.py
Expand Up @@ -86,14 +86,15 @@ def statevector_expr(
## If a QuantumCircuit is passed, extract the statevector

if isinstance(statevector, QuantumCircuit):
circuit = statevector.copy()
# Run the circuit in statevector_simulator and obtain the final state statevector
simulator = AerSimulator(method='statevector')

# Append an instruction to save the statevector of the final state of the circuit
circuit.save_statevector()

# Transpile and run the circuit
circuit = transpile(statevector, backend=simulator)
circuit = transpile(circuit, backend=simulator)
statevector = np.asarray(simulator.run(circuit).result().data()['statevector'])

## Setup
Expand Down

0 comments on commit ec9b3fa

Please sign in to comment.