# import functionality from qiskit for building and running circuits
from qiskit import QuantumCircuit, BasicAer, transpile

# here we'll create a GHZ state
circuit = QuantumCircuit(3)
circuit.h(0)
circuit.cnot(0,1)
circuit.cnot(1,2)
circuit.measure_all()

print(circuit.draw())

# select a quantum backend and run the circuit
backend = BasicAer.get_backend('qasm_simulator')
shots = 2**13

job = backend.run(transpile(circuit, backend), shots=shots)
result = job.result()
counts = result.get_counts()

from qiskit.visualization import plot_histogram
plot_histogram(counts)

# defining the state |1>
import numpy as np
a = 0
b = 1
state = np.array([a,b])
norm = np.linalg.norm(state)
normalized_state_ONE = state/norm

print(normalized_state_ONE)

# define the state |+>
a = 0.70710678 # 1/sqrt(2)
b = 0.70710678
state = np.array([a,b])
norm = np.linalg.norm(state)
normalized_state_PLUS = state/norm

print(normalized_state_PLUS)

# define the Pauli X operator
x = np.array([[0, 1],
             [1, 0]])

from qiskit.visualization import plot_state_city, plot_bloch_multivector
x_eigenvals, x_eigenvecs = np.linalg.eigh(x)

print('\nEigensystem of X:')
print(x_eigenvals)
print(x_eigenvecs)

plot_bloch_multivector(x_eigenvecs[:,1])

circuit = QuantumCircuit(1)
circuit.initialize(normalized_state_ONE)
circuit.h(0)
circuit.measure_all()
print(circuit)

job = backend.run(transpile(circuit, backend), shots=shots)
result = job.result()

counts = result.get_counts()
print(counts)

expval = (counts.get('0',0)-counts.get('1',0)) / shots
print('expectation value: {}'.format(expval))

circuit = QuantumCircuit(1)
circuit.initialize(normalized_state_PLUS)
circuit.h(0)
circuit.measure_all()
print(circuit)

job = backend.run(transpile(circuit, backend), shots=shots)
result = job.result()

counts = result.get_counts()
print(counts)

expval = (counts.get('0',0)-counts.get('1',0)) / shots
print('expectation value: {}'.format(expval))

circuit = QuantumCircuit(1)
circuit.initialize(normalized_state_ONE)
circuit.sdg(0)
circuit.h(0)
circuit.measure_all()
print(circuit)

job = backend.run(transpile(circuit, backend), shots=shots)
result = job.result()
counts = result.get_counts()
print(counts)
expval = (counts.get('0',0)-counts.get('1',0)) / shots
print('expectation value: {}'.format(expval))

circuit = QuantumCircuit(1)
circuit.initialize(normalized_state_PLUS)
circuit.sdg(0)
circuit.h(0)
circuit.measure_all()
print(circuit)

job = backend.run(transpile(circuit, backend), shots=shots)
result = job.result()
counts = result.get_counts()
print(counts)
expval = (counts.get('0',0)-counts.get('1',0)) / shots
print('expectation value: {}'.format(expval))

import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright