Exemple #1
0
    def forward(self, H_C_ls):
        """
        Forward propagation
        """
        cir = UAnsatz(self.num_qubits)
        cir.complex_entangled_layer(self.theta, self.p)
        cir.run_state_vector()
        loss = cir.expecval(H_C_ls)

        return loss, cir
Exemple #2
0
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
Exemple #3
0
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