import matplotlib.pyplot as plt
import numpy as np
from math import pi

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, transpile
from qiskit.tools.visualization import circuit_drawer
from qiskit.quantum_info import state_fidelity
from qiskit import BasicAer

backend = BasicAer.get_backend('unitary_simulator')

q = QuantumRegister(1)

qc = QuantumCircuit(q)
qc.u(pi/2, pi/4,pi/8,q)
qc.draw()

job =backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#Pゲート
qc =QuantumCircuit(q)
qc.p(pi/2, q)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#恒等ゲートId = p(0)
qc = QuantumCircuit(q)
qc.id(q)
qc.draw()

job =backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#パウリゲート
qc = QuantumCircuit(q)
qc.x(q)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#ビットアンドフェーズフリップゲート
qc = QuantumCircuit(q)
qc.y(q)
qc.draw()

job =backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#S=√Zフェーズ
qc = QuantumCircuit(q)
qc.s(q)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#H アダマールゲート
qc = QuantumCircuit(q)
qc.h(q)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#S転置共役
qc = QuantumCircuit(q)
qc.sdg(q)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

qc = QuantumCircuit(q)
qc.t(q)
qc.draw()

job =backend.run(transpile(qc,backend))
job.result().get_unitary(qc, decimals=3)

#標準回転
qc = QuantumCircuit(q)
qc.rx(pi/2, q)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#Y軸まわりの回転
qc = QuantumCircuit(q)
qc.ry(pi/2, q)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#Zまわりの回転
qc= QuantumCircuit(q)
qc.rz(pi/2,q)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#2量子ビット場合
q= QuantumRegister(2)

#制御パウリゲート
qc = QuantumCircuit(q)
qc.cx(q[0],q[1])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#制御Ｙゲート
qc = QuantumCircuit(q)
qc.cy(q[0],q[1])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#制御Ｚゲート
qc = QuantumCircuit(q)
qc.cz(q[0],q[1])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#制御回転ゲート
qc = QuantumCircuit(q)
qc.crz(pi/2,q[0],q[1])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#制御位相回転
qc = QuantumCircuit(q)
qc.cp(pi/2,q[0],q[1])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#制御u回転
qc = QuantumCircuit(q)
qc.cu(pi/2,pi/2,pi/2,0,q[0],q[1])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#SWAPGETA
qc = QuantumCircuit(q)
qc.swap(q[0],[1])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

q = QuantumRegister(3)

#ToffoliゲートCXXゲート
qc = QuantumCircuit(q)
qc.ccx(q[0],q[1],q[2])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#CSWAPゲート
qc = QuantumCircuit(q)
qc.cswap(q[0],q[1],q[2])
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_unitary(qc, decimals=3)

#非ユニタリ操作
q = QuantumRegister(1)
c = ClassicalRegister(1)

qc = QuantumCircuit(q, c)
qc.measure(q,c)
qc.draw()

backend =BasicAer.get_backend('qasm_simulator')
job = backend.run(transpile(qc,backend))
job.result().get_counts(qc)

qc = QuantumCircuit(q,c)
qc.h(q)
qc.measure(q,c)
qc.draw()

job = backend.run(transpile(qc,backend))
job.result().get_counts(qc)

#reset
qc = QuantumCircuit(q,c)
qc.h(q)
qc.reset(q[0])
qc.measure(q,c)
qc.draw()

job = backend.run(transpile(qc, backend))
job.result().get_counts(qc)

qc = QuantumCircuit(q,c)
qc.h(q)
qc.measure(q,c)
qc.x(q[0]).c_if(c,0)
qc.measure(q, c)
qc.draw()

import math
desired_vector = [
    1 / math.sqrt(16) * complex(0, 1),
    1 / math.sqrt(8) * complex(1, 0),
    1 / math.sqrt(16) * complex(1, 1),
    0,
    0,
    1 / math.sqrt(8) * complex(1, 2),
    1 / math.sqrt(16) * complex(1, 0),
    0]


q = QuantumRegister(3)

qc = QuantumCircuit(q)

qc.initialize(desired_vector, [q[0],q[1],q[2]])
qc.draw()

backend = BasicAer.get_backend('statevector_simulator')
job =backend.run(transpile(qc, backend))
qc_state = job.result().get_statevector(qc)
qc_state

state_fidelity(desired_vector, qc_state)

import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright

