%reset -f
import matplotlib.font_manager
import numpy as np
from matplotlib import pyplot as plt
font = {
    'family':'SimHei',
    'weight':'bold',
    'size':'16'
}
matplotlib.rc("font",**font)
from IPython.display import display_pretty
import warnings
warnings.filterwarnings("ignore")

from qutip import *

q = (1/np.sqrt(7))*Qobj(np.array([[1+ 1j], [1-2j]]).T)
q.conj().full()

p=(1/np.sqrt(2))*Qobj(np.array([[-1j], [1]]))
p.full()

q.conj()*p

from sympy import Matrix, symbols, sqrt, init_printing
from sympy.physics.quantum import TensorProduct
from IPython.display import display_pretty

init_printing(use_latex=True)
a1 = symbols('a_1')
b1 = symbols('b_1')
a2 = symbols('a_2')
b2 = symbols('b_2')
# psi1 = Matrix(2,1,[a1,b1])
# psi2 = Matrix(2,1,[a2,b2])

U_I = Matrix([[1,0],
              [0,1]])
U_H = 1/sqrt(2)*Matrix([[1, 1],
                        [1,-1]])
U_Z=Matrix(2,2,[1,0,0,-1])
Cnot=Matrix([[1,0,0,0],
             [0,1,0,0],
             [0,0,0,1],
             [0,0,1,0]])

psi1 = Matrix(2,1,[1,0])
psi2 = Matrix(2,1,[1,0])

psi12 = TensorProduct(psi1,psi2)
U_FINAL= Cnot*(TensorProduct(U_I,U_Z)*TensorProduct(U_H,U_H))*psi12

U_FINAL

from sympy import Matrix, symbols, sqrt, init_printing
from sympy.physics.quantum import TensorProduct
from IPython.display import display_pretty

init_printing(use_latex=True)
a1 = symbols('a_1')
b1 = symbols('b_1')
a2 = symbols('a_2')
b2 = symbols('b_2')

U_I = Matrix([[1,0],
              [0,1]])
U_H = 1/sqrt(2)*Matrix([[1, 1],
                        [1,-1]])
U_Z=Matrix(2,2,[1,0,0,-1])
U_X=Matrix(2,2,[0,1,1,0])
Cnot=Matrix([[1,0,0,0],
             [0,1,0,0],
             [0,0,0,1],
             [0,0,1,0]])

psi0 = Matrix(2,1,[1,0])
psi1 = Matrix(2,1,[0,1])
psi3 = a1*psi0 + b1*psi1

psi3
# psi1 = Matrix(2,1,[a1,b1])
# psi2 = Matrix(2,1,[a2,b2])
# psi12 = TensorProduct(psi1,psi2)
# # psi12 = psi1+ psi2
U_FINAL= TensorProduct(Cnot,U_I)*(TensorProduct(U_X,U_I,U_Z)*TensorProduct(Cnot,U_I))* TensorProduct(U_H,U_Z,U_I)
U_FINAL * TensorProduct(psi0, psi1, psi0)

from sympy import sqrt, symbols, Rational
from sympy import expand, Eq, Symbol, simplify, exp, sin, srepr
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *
from sympy.physics.quantum.gate import *
from sympy.physics.quantum.grover import *
from sympy.physics.quantum.qft import QFT, IQFT, Fourier
from sympy.physics.quantum.circuitplot import circuit_plot

for gate in [X,Y,Z]:
    for state in [Qubit('0'),Qubit('1')]:
        lhs = gate(0)*state
        rhs = qapply(lhs)
        display(Eq(lhs,rhs))

%matplotlib inline
import numpy as np
from warnings import filterwarnings
filterwarnings(action='ignore', category=DeprecationWarning)


phi_plus = np.array([1, 0, 0, 1])/np.sqrt(2)   # | Phi^+ >
phi_minus = np.array([1, 0, 0, -1])/np.sqrt(2) # | Phi^- >
psi_plus = np.array([0, 1, 1, 0])/np.sqrt(2)   # | Psi^+ >
psi_minus = np.array([0, 1, -1, 0])/np.sqrt(2) # | Psi^- >


import torch
initial_state = [1,0]   # Define initial_state as |0>
# initial_state = [0,1]   # Define initial_state as |0>
# initial_state = [1/np.sqrt(2), 1/np.sqrt(2)]  # Define state |q_0>

initial_state_torch = torch.tensor([initial_state])
x_gate_matrix = torch.tensor([[0,1],[1,0]])
out_state_torch = torch.mm(x_gate_matrix,initial_state_torch.t()) 
print("after X matrix, the state is ",out_state_torch.t()) # Display the output state vector

import numpy
from numpy import pi as PI
import paddle 
import paddle_quantum
from paddle import matmul
from paddle_quantum.ansatz import Circuit
from paddle_quantum.qinfo import random_pauli_str_generator, pauli_str_to_matrix
from paddle_quantum.linalg import dagger

N = 1  # Number of qubits
SEED = np.random.randint(9999)  # Fixed random seed
# Generate random Hamiltonian represented by Pauli string
numpy.random.seed(SEED)
hamiltonian = random_pauli_str_generator(N, terms=1)
print("Random Hamiltonian in Pauli string format = \n", hamiltonian)

# Generate matrix representation of Hamiltonian
complex_dtype = paddle_quantum.get_dtype()
H = pauli_str_to_matrix(hamiltonian, N).astype(complex_dtype)
H

