def U_theta(theta, n, depth): """ :param theta: dim: [n, depth + 3] :param n: number of qubits :param depth: circuit depth :return: U_theta """ # 初始化网络 cir = UAnsatz(n) # 先搭建广义的旋转层 for i in range(n): cir.rz(theta[i][0], i) cir.ry(theta[i][1], i) cir.rz(theta[i][2], i) # 默认深度为 depth = 1 # 搭建纠缠层和 Ry旋转层 for d in range(3, depth + 3): for i in range(n-1): cir.cnot([i, i + 1]) cir.cnot([n-1, 0]) for i in range(n): cir.ry(theta[i][d], i) return cir.U
def U_theta(theta, N): """ U_theta """ cir = UAnsatz(N) # ============== D1=2 ============== cir.ry(theta[0], 2) cir.rz(theta[1], 2) cir.cnot([2, 1]) cir.ry(theta[2], 2) cir.rz(theta[3], 2) cir.cnot([2, 1]) # ============== D2=2 ============== cir.ry(theta[4], 1) cir.ry(theta[5], 2) cir.rz(theta[6], 1) cir.rz(theta[7], 2) cir.cnot([1, 2]) cir.ry(theta[8], 1) cir.ry(theta[9], 2) cir.rz(theta[10], 1) cir.rz(theta[11], 2) cir.cnot([1, 2]) return cir.state
def Encoder(theta): # use UAnsatz to initialize the network cir = UAnsatz(N) for layer_num in range(cir_depth): for which_qubit in range(N): cir.ry(theta[block_len * layer_num * N + which_qubit], which_qubit) cir.rz(theta[(block_len * layer_num + 1) * N + which_qubit], which_qubit) for which_qubit in range(N - 1): cir.cnot([which_qubit, which_qubit + 1]) cir.cnot([N - 1, 0]) return cir.U
def Encoder(theta): # 用 UAnsatz 初始化网络 cir = UAnsatz(N) # 搭建层级结构: for layer_num in range(cir_depth): for which_qubit in range(N): cir.ry(theta[block_len * layer_num * N + which_qubit], which_qubit) cir.rz(theta[(block_len * layer_num + 1) * N + which_qubit], which_qubit) for which_qubit in range(N - 1): cir.cnot([which_qubit, which_qubit + 1]) cir.cnot([N - 1, 0]) return cir.U
def U_theta(initial_state, theta, N, D): """ Quantum Neural Network """ # Initialize the quantum neural network by the number of qubits (width of the network) cir = UAnsatz(N) # Use in-built template (R_y + CNOT) cir.real_entangled_layer(theta[:D], D) # Add a layer of R_y rotation gate for i in range(N): cir.ry(theta=theta[D][i][0], which_qubit=i) # Act quantum neural network on initilized state final_state = cir.run_density_matrix(initial_state) return final_state
def U_theta(initial_state, theta, N, D): """ Quantum Neural Network """ # 按照量子比特数量/网络宽度初始化量子神经网络 cir = UAnsatz(N) # 内置的 {R_y + CNOT} 电路模板 cir.real_entangled_layer(theta[:D], D) # 铺上最后一列 R_y 旋转门 for i in range(N): cir.ry(theta=theta[D][i][0], which_qubit=i) # 量子神经网络作用在给定的初始态上 final_state = cir.run_density_matrix(initial_state) return final_state
def U_theta(theta, Hamiltonian, N, D): """ Quantum Neural Network """ # Initialize the quantum neural network by the number of qubits (width of the network) cir = UAnsatz(N) # Use built-in template (R_y + CNOT) cir.real_entangled_layer(theta[:D], D) # Add a layer of R_y rotation gates for i in range(N): cir.ry(theta=theta[D][i][0], which_qubit=i) # Act QNN on the default initial state |0000> cir.run_state_vector() # Calculate the expectation value of the given Hamiltonian expectation_val = cir.expecval(Hamiltonian) return expectation_val
def U_theta(theta, input_state, N, D): """ Circuit """ cir = UAnsatz(N, input_state=input_state) for i in range(N): cir.rz(theta=theta[0][0][i], which_qubit=i + 1) cir.ry(theta=theta[0][1][i], which_qubit=i + 1) cir.rz(theta=theta[0][2][i], which_qubit=i + 1) for repeat in range(D): for i in range(1, N): cir.cnot(control=[i, i + 1]) for i in range(N): cir.ry(theta=theta[repeat][0][i], which_qubit=i + 1) cir.ry(theta=theta[repeat][1][i], which_qubit=i + 1) cir.rz(theta=theta[repeat][2][i], which_qubit=i + 1) return cir.state
def U_theta(theta, n, depth): """ :param theta: dim: [n, depth + 3] :param n: number of qubits :param depth: circuit depth :return: U_theta """ cir = UAnsatz(n) for i in range(n): cir.rz(theta[i][0], i) cir.ry(theta[i][1], i) cir.rz(theta[i][2], i) for d in range(3, depth + 3): for i in range(n - 1): cir.cnot([i, i + 1]) cir.cnot([n - 1, 0]) for i in range(n): cir.ry(theta[i][d], i) return cir.U
def U_theta(theta, Hamiltonian, N, D): """ Quantum Neural Network """ # 按照量子比特数量/网络宽度初始化量子神经网络 cir = UAnsatz(N) # 内置的 {R_y + CNOT} 电路模板 cir.real_entangled_layer(theta[:D], D) # 铺上最后一列 R_y 旋转门 for i in range(N): cir.ry(theta=theta[D][i][0], which_qubit=i) # 量子神经网络作用在默认的初始态 |0000>上 cir.run_state_vector() # 计算给定哈密顿量的期望值 expectation_val = cir.expecval(Hamiltonian) return expectation_val
def U_theta(theta, input_state, N, D): # definition of U_theta """ :param theta: :param input_state: :return: """ cir = UAnsatz(N, input_state=input_state) for i in range(N): cir.rx(theta=theta[0][0][i], which_qubit=i + 1) cir.ry(theta=theta[0][1][i], which_qubit=i + 1) cir.rx(theta=theta[0][2][i], which_qubit=i + 1) for repeat in range(D): for i in range(1, N): cir.cnot(control=[i, i + 1]) for i in range(N): cir.ry(theta=theta[repeat][0][i], which_qubit=i + 1) # cir.ry(theta=theta[repeat][1][i], which_qubit=i + 1) # cir.ry(theta=theta[repeat][2][i], which_qubit=i + 1) return cir.state