Beispiel #1
0
def set_circuit_rhfZ(n_qubits, n_electrons):
    """Function:
    Construct circuit for RHF |0000...1111> with one ancilla

    Author(s): Takashi Tsuchimochi
    """
    circuit = QuantumCircuit(n_qubits)
    for i in range(n_electrons):
        circuit.add_X_gate(i)
    return circuit
Beispiel #2
0
def set_circuit_rhf(n_qubit_system, n_electrons):
    """Function:
    Construct circuit for RHF |0000...1111>

    Author(s): Yuto Mori
    """
    circuit = QuantumCircuit(n_qubit_system)
    for i in range(n_electrons):
        circuit.add_X_gate(i)
    return circuit
Beispiel #3
0
def make_gate(n, index, pauli_id):
    """ゲートを作る関数"""
    circuit = QuantumCircuit(n)
    for i in range(len(index)):
        gate_number = index[i]
        if pauli_id[i] == 1:
            circuit.add_X_gate(gate_number)
        elif pauli_id[i] == 2:
            circuit.add_Y_gate(gate_number)
        elif pauli_id[i] == 3:
            circuit.add_Z_gate(gate_number)
    return circuit
Beispiel #4
0
def set_circuit_rohf(n_qubit_system, noa, nob):
    """Function:
    Construct circuit for ROHF |0000...10101111>

    Author(s): Yuto Mori, Takashi Tsuchimochi
    """
    circuit = QuantumCircuit(n_qubit_system)
    for i in range(noa):
        circuit.add_X_gate(2 * i)
    for i in range(nob):
        circuit.add_X_gate(2 * i + 1)
    return circuit
Beispiel #5
0
def set_circuit_rohfZ(n_qubits, noa, nob):
    """Function:
    Construct circuit for ROHF |0000...10101111> with one ancilla

    Author(s): Takashi Tsuchimochi
    """
    # generate circuit for rhf
    circuit = QuantumCircuit(n_qubits)
    for i in range(noa):
        circuit.add_X_gate(2 * i)
    for i in range(nob):
        circuit.add_X_gate(2 * i + 1)
    return circuit
Beispiel #6
0
        def func():
            def copy_circuit(c):
                ret = QuantumCircuit(2)
                for i in range(c.get_gate_count()):
                    gate = c.get_gate(i)
                    ret.add_gate(gate)
                return ret

            circuit = QuantumCircuit(2)
            circuit.add_X_gate(0)
            circuit.add_Y_gate(1)
            copied = copy_circuit(circuit)
            return copied
Beispiel #7
0
def qtest(angle):
    state = QuantumState(3)
    state.set_Haar_random_state()
    circuit = QuantumCircuit(3)
    circuit.add_X_gate(0)
    merged_gate = merge(CNOT(0, 1), Y(1))
    circuit.add_gate(merged_gate)
    circuit.add_RX_gate(1, angle)
    circuit.update_quantum_state(state)
    observable = Observable(3)
    observable.add_operator(2.0, "X 2 Y 1 Z 0")
    observable.add_operator(-3.0, "Z 2")
    result = observable.get_expectation_value(state)

    output = {'energy': result}
    return (output)
Beispiel #8
0
def cost_phf_sample(Quket, print_level,
                    qulacs_hamiltonian, qulacs_hamiltonianZ, qulacs_s2Z,
                    qulacs_ancZ, coef0_H, coef0_S2, ref, theta_list,
                    samplelist):
    """Function:
    Sample Hamiltonian and S**2 expectation values with PHF and PUCCSD.
    Write out the statistics in csv files.

    Author(s): Takashi Tsuchimochi
    """
    t1 = time.time()

    noa = Quket.noa
    nob = Quket.nob
    nva = Quket.nva
    nvb = Quket.nvb
    n_electrons = Quket.n_electrons
    n_qubit_system = n_qubits
    n_qubits = Quket.n_qubits + 1
    anc = n_qubit_system
    ndim1 = Quket.ndim1

    state = QuantumState(n_qubits)
    circuit_rhf = set_circuit_rhfZ(n_qubits, n_electrons)
    circuit_rhf.update_quantum_state(state)

    if ref == "phf":
        circuit_uhf = set_circuit_uhfZ(n_qubits, noa, nob, nva, nvb, theta_list)
        circuit_uhf.update_quantum_state(state)
        print("pHF")
    elif ref == "puccsd":
        circuit = set_circuit_uccsd(n_qubits, noa, nob, nva, nvb, theta_list,
                                    ndim1)
        for i in range(rho):
            circuit.update_quantum_state(state)
        print("UCCSD")

    if print_level > -1:
        print("State before projection")
        utils.print_state(state, n_qubit_system)
    #### Set post-measurement states ####
    #poststate0 = state.copy()
    #poststate1 = state.copy()
    #circuit0 = QuantumCircuit(n_qubits)
    #circuit1 = QuantumCircuit(n_qubits)
    #### Projection to anc = 0 or anc = 1 ###
    #circuit0.add_gate(P0(0))
    #circuit1.add_gate(P1(0))
    #circuit0.update_quantum_state(poststate0)
    #circuit1.update_quantum_state(poststate1)
    #### Renormalize each state ###
    #norm0 = poststate0.get_squared_norm()
    #norm1 = poststate1.get_squared_norm()
    #poststate0.normalize(norm0)
    #poststate1.normalize(norm1)

    ### grid loop ###
    Ng = 4
    beta = [-0.861136311594053, -0.339981043584856,
             0.339981043584856,  0.861136311594053]
    wg = [0.173927422568724, 0.326072577431273,
          0.326072577431273, 0.173927422568724]

    Ng = 2
    beta = [0.577350269189626, -0.577350269189626]
    wg = [0.5, 0.5]

    ### a list to compute the probability to observe 0 in ancilla qubit
    p0_list = np.full(n_qubits, 2)
    p0_list[-1] = 0

    ### Array for <HUg>, <S2Ug>, <Ug>
    # samplelist = [10,100,1000,10000,100000,1000000,10000000]
    ncyc = 4
    prints("", filepath="./log2.txt")
    for i_sample in samplelist:
        i_sample_x = i_sample
        if i_sample == 10000000:
            print("OK")
            ncyc = ncyc*10
            i_sample_x = 1000000
        sampleHUg1 = []
        sampleHUg2 = []
        sampleHUg3 = []
        sampleHUg4 = []
        sampleS2Ug1 = []
        sampleS2Ug2 = []
        sampleS2Ug3 = []
        sampleS2Ug4 = []
        sampleUg1 = []
        sampleUg2 = []
        sampleUg3 = []
        sampleUg4 = []
        # sampleEn = []
        # sampleS2 = []
        sampleHUg = np.zeros((ncyc, Ng))
        sampleS2Ug = np.zeros((ncyc, Ng))
        sampleUg = np.zeros((ncyc, Ng))
        sampleEn = np.zeros((ncyc, 1))
        sampleS2 = np.zeros((ncyc, 1))

        for icyc in range(ncyc):
            prints(f"n_sample = {i_sample_x}  ({icyc} / {ncyc})",
                   filepath="./log2.txt")
            HUg = []
            S2Ug = []
            Ug = []
            Ephf = S2 = Norm = 0
            for i in range(Ng):
                ### Copy quantum state of UHF (cannot be done in real device) ###
                state_g = QuantumState(n_qubits)
                state_g.load(state)
                ### Construct Ug test
                circuit_ug = QuantumCircuit(n_qubits)
                ### Hadamard on anc
                circuit_ug.add_H_gate(anc)
                controlled_Ug(circuit_ug, n_qubits, anc, np.arccos(beta[i]))
                circuit_ug.add_H_gate(anc)
                circuit_ug.update_quantum_state(state_g)

                ### Set post-measurement states ####
                poststate0 = state_g.copy()
                poststate1 = state_g.copy()
                circuit0 = QuantumCircuit(n_qubits)
                circuit1 = QuantumCircuit(n_qubits)
                ### Projection to anc = 0 or anc = 1 ###
                circuit0.add_gate(P0(anc))
                circuit1.add_gate(P1(anc))
                circuit0.update_quantum_state(poststate0)
                circuit1.update_quantum_state(poststate1)
                ### Renormalize each state ###
                norm0 = poststate0.get_squared_norm()
                norm1 = poststate1.get_squared_norm()
                poststate0.normalize(norm0)
                poststate1.normalize(norm1)
                ### Set ancilla qubit of poststate1 to zero (so that it won't be used) ###
                circuit_anc = QuantumCircuit(n_qubits)
                circuit_anc.add_X_gate(anc)
                circuit_anc.update_quantum_state(poststate1)
                print(
                        test_transition_observable(
                            state_g, qulacs_hamiltonianZ,
                            poststate0, poststate1, 100000))
                # exit()

                ### Probabilities for getting 0 and 1 in ancilla qubit ###
                p0 = state_g.get_marginal_probability(p0_list)
                p1 = 1 - p0

                ### Compute expectation value <HUg> ###
                HUg.append(sample_observable(state_g,
                                             qulacs_hamiltonianZ,
                                             i_sample_x).real)
                #HUg.append(adaptive_sample_observable(state_g,
                #                                      qulacs_hamiltonianZ,
                #                                      i_sample_x).real)
                ### <S2Ug> ###
                S2Ug.append(sample_observable(state_g,
                                              qulacs_s2Z,
                                              i_sample_x).real)
                #S2Ug.append(adaptive_sample_observable(state_g,
                #                                       qulacs_s2Z,
                #                                       i_sample_x).real)
                #S2Ug.append(qulacs_s2Z.get_expectation_value(state_g))
                #HUg.append(0)
                #S2Ug.append(0)

                #Ug.append(p0 - p1)
                n_term = qulacs_hamiltonianZ.get_term_count()
                n_sample_total = i_sample_x * n_term
                # in the worst-case scenario,
                # Ug is measured as many times as n_sample_total
                #(required to evaluate HUg)
                Ug.append(sample_observable(state_g,
                                            qulacs_ancZ,
                                            i_sample_x*n_term).real)
                #p0_sample = 0
                #for j_sample in range(n_sample_total):
                #   if(p0 > np.random.rand()):
                #      p0_sample += 1
                #Ug.append(2*p0_sample/n_sample_total - 1)
                ### Norm accumulation ###
                Norm += wg[i]*Ug[i]
                sampleHUg[icyc, i] = HUg[i]
                sampleS2Ug[icyc, i] = S2Ug[i]
                sampleUg[icyc, i] = Ug[i]
            #print('p0 : ',p0,'  p1 : ',p1,  '  p0 - p1 : ',p0-p1)

            sampleHUg1.append(HUg[0])
            sampleHUg2.append(HUg[1])
            #sampleHUg3.append(HUg[2])
            #sampleHUg4.append(HUg[3])
            sampleS2Ug1.append(S2Ug[0])
            sampleS2Ug2.append(S2Ug[1])
            #sampleS2Ug3.append(S2Ug[2])
            #sampleS2Ug4.append(S2Ug[3])
            sampleUg1.append(Ug[0])
            sampleUg2.append(Ug[1])
            #sampleUg3.append(Ug[2])
            #sampleUg4.append(Ug[3])

            ### Energy calculation <HP>/<P> and <S**2P>/<P> ###
            Ephf = 0
            for i in range(Ng):
                Ephf += wg[i]*HUg[i]/Norm
                S2 += wg[i]*S2Ug[i]/Norm
            # print(" <S**2> = ", S2, '\n')
            Ephf += coef0_H
            S2 += coef0_S2
            sampleEn[icyc, 0] = Ephf
            sampleS2[icyc, 0] = S2
            # print(" <E[PHF]> (Nsample = ",i_sample,") = ", Ephf)
        #print(f"(n_sample = {i_sample}):  sample HUg1\n",sampleHUg1)
        #print(f"(n_sample = {i_sample}):  sample HUg2\n",sampleHUg2)
        #print(f"(n_sample = {i_sample}):  sample HUg3\n",sampleHUg3)
        #print(f"(n_sample = {i_sample}):  sample HUg4\n",sampleHUg4)
        #print(f"(n_sample = {i_sample}):  sample S2Ug1\n",sampleS2Ug1)
        #print(f"(n_sample = {i_sample}):  sample S2Ug2\n",sampleS2Ug2)
        #print(f"(n_sample = {i_sample}):  sample S2Ug3\n",sampleS2Ug3)
        #print(f"(n_sample = {i_sample}):  sample S2Ug4\n",sampleS2Ug4)
        #print(f"(n_sample = {i_sample}):  sample Ug1\n",sampleUg1)
        #print(f"(n_sample = {i_sample}):  sample Ug2\n",sampleUg2)
        #print(f"(n_sample = {i_sample}):  sample Ug3\n",sampleUg3)
        #print(f"(n_sample = {i_sample}):  sample Ug4\n",sampleUg4)
        #print(f"(n_sample = {i_sample}):  sample HUg1\n",sampleHUg1)
        #print(f"(n_sample = {i_sample}):  sample HUg2\n",sampleHUg2)
        #print(f"(n_sample = {i_sample}):  sample HUg3\n",sampleHUg3)
        #print(f"(n_sample = {i_sample}):  sample HUg4\n",sampleHUg4)
        #print(f"(n_sample = {i_sample}):  sample En\n",sampleEn)
        #print(f"(n_sample = {i_sample}):  sample S2\n",sampleS2)
        with open(f"./Ug_{i_sample}.csv", "w") as fUg:
            writer = csv.writer(fUg)
            writer.writerows(sampleUg)
        with open(f"./HUg_{i_sample}.csv", "w") as fHUg:
            writer = csv.writer(fHUg)
            writer.writerows(sampleHUg)
        with open(f"./S2Ug_{i_sample}.csv", "w") as fS2Ug:
            writer = csv.writer(fS2Ug)
            writer.writerows(sampleS2Ug)
        with open(f"./En_{i_sample}.csv", "w") as fEn:
            writer = csv.writer(fEn)
            writer.writerows(sampleEn)
        with open(f"./S2_{i_smaple}.csv", "w") as fS2:
            writer = csv.writer(fS2)
            writer.writerows(sampleS2)
    return Ephf, S2
Beispiel #9
0
    def _try_append_gate(self, op: ops.GateOperation,
                         qulacs_circuit: qulacs.QuantumCircuit,
                         indices: np.array):
        # One qubit gate
        if isinstance(op.gate, ops.pauli_gates._PauliX):
            qulacs_circuit.add_X_gate(indices[0])
        elif isinstance(op.gate, ops.pauli_gates._PauliY):
            qulacs_circuit.add_Y_gate(indices[0])
        elif isinstance(op.gate, ops.pauli_gates._PauliZ):
            qulacs_circuit.add_Z_gate(indices[0])
        elif isinstance(op.gate, ops.common_gates.HPowGate):
            qulacs_circuit.add_H_gate(indices[0])
        elif isinstance(op.gate, ops.common_gates.XPowGate):
            qulacs_circuit.add_RX_gate(indices[0], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.common_gates.YPowGate):
            qulacs_circuit.add_RY_gate(indices[0], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.common_gates.ZPowGate):
            qulacs_circuit.add_RZ_gate(indices[0], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.SingleQubitMatrixGate):
            mat = op.gate._matrix
            qulacs_circuit.add_dense_matrix_gate(indices[0], mat)
        elif isinstance(op.gate, circuits.qasm_output.QasmUGate):
            lmda = op.gate.lmda
            theta = op.gate.theta
            phi = op.gate.phi
            gate = qulacs.gate.U3(indices[0], theta * np.pi, phi * np.pi,
                                  lmda * np.pi)
            qulacs_circuit.add_gate(gate)

        # Two qubit gate
        elif isinstance(op.gate, ops.common_gates.CNotPowGate):
            if op.gate._exponent == 1.0:
                qulacs_circuit.add_CNOT_gate(indices[0], indices[1])
            else:
                mat = _get_google_rotx(op.gate._exponent)
                gate = qulacs.gate.DenseMatrix(indices[1], mat)
                gate.add_control_qubit(indices[0], 1)
                qulacs_circuit.add_gate(gate)
        elif isinstance(op.gate, ops.common_gates.CZPowGate):
            if op.gate._exponent == 1.0:
                qulacs_circuit.add_CZ_gate(indices[0], indices[1])
            else:
                mat = _get_google_rotz(op.gate._exponent)
                gate = qulacs.gate.DenseMatrix(indices[1], mat)
                gate.add_control_qubit(indices[0], 1)
                qulacs_circuit.add_gate(gate)
        elif isinstance(op.gate, ops.common_gates.SwapPowGate):
            if op.gate._exponent == 1.0:
                qulacs_circuit.add_SWAP_gate(indices[0], indices[1])
            else:
                qulacs_circuit.add_dense_matrix_gate(indices, op._unitary_())
        elif isinstance(op.gate, ops.parity_gates.XXPowGate):
            qulacs_circuit.add_multi_Pauli_rotation_gate(
                indices, [1, 1], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.parity_gates.YYPowGate):
            qulacs_circuit.add_multi_Pauli_rotation_gate(
                indices, [2, 2], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.parity_gates.ZZPowGate):
            qulacs_circuit.add_multi_Pauli_rotation_gate(
                indices, [3, 3], -np.pi * op.gate._exponent)
        elif isinstance(op.gate, ops.TwoQubitMatrixGate):
            indices.reverse()
            mat = op.gate._matrix
            qulacs_circuit.add_dense_matrix_gate(indices, mat)

            # Three qubit gate
            """
            # deprecated because these functions cause errors in gpu
            elif isinstance(op.gate, ops.three_qubit_gates.CCXPowGate):
                mat = _get_google_rotx(op.gate._exponent)
                gate = qulacs.gate.DenseMatrix(indices[2], mat)
                gate.add_control_qubit(indices[0],1)
                gate.add_control_qubit(indices[1],1)
                qulacs_circuit.add_gate(gate)
            elif isinstance(op.gate, ops.three_qubit_gates.CCZPowGate):
                mat = _get_google_rotz(op.gate._exponent)
                gate = qulacs.gate.DenseMatrix(indices[2], mat)
                gate.add_control_qubit(indices[0],1)
                gate.add_control_qubit(indices[1],1)
                qulacs_circuit.add_gate(gate)
            """
        elif isinstance(op.gate, ops.three_qubit_gates.CSwapGate):
            mat = np.zeros(shape=(4, 4))
            mat[0, 0] = 1
            mat[1, 2] = 1
            mat[2, 1] = 1
            mat[3, 3] = 1
            gate = qulacs.gate.DenseMatrix(indices[1:], mat)
            gate.add_control_qubit(indices[0], 1)
            qulacs_circuit.add_gate(gate)

        # Misc
        elif protocols.has_unitary(op):
            indices.reverse()
            mat = op._unitary_()
            qulacs_circuit.add_dense_matrix_gate(indices, mat)

        # Not unitary
        else:
            return False

        return True
Beispiel #10
0
def main():
    ## Example1
    circuit = QuantumCircuit(3)
    circuit.add_X_gate(0)
    circuit.add_Y_gate(1)
    circuit.add_Z_gate(2)
    circuit.add_dense_matrix_gate([0,1], [[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]])
    circuit.add_CNOT_gate(2,0)
    circuit.add_X_gate(2)
    draw_circuit(circuit, verbose=1)

    ## Example2
    circuit = QuantumCircuit(3)
    circuit.add_X_gate(0)
    circuit.add_Y_gate(1)
    circuit.add_dense_matrix_gate([0,1], [[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]])
    circuit.add_Z_gate(2)
    circuit.add_CNOT_gate(2,0)
    circuit.add_X_gate(2)
    draw_circuit(circuit, verbose=1)

    ## Example3
    circuit = QuantumCircuit(3)
    circuit.add_X_gate(1)
    circuit.add_CZ_gate(0,2)
    circuit.add_X_gate(1)
    draw_circuit(circuit)

    ## Example4
    circuit = QuantumCircuit(4)
    ##CCX0,2, 3
    cx_gate = CNOT(2,3)
    cx_mat_gate = to_matrix_gate(cx_gate)
    control_index = 0
    control_with_value = 1
    cx_mat_gate.add_control_qubit(control_index, control_with_value)
    circuit.add_gate(cx_mat_gate)
    ##CCX1,2, 3
    ccx = TOFFOLI(1,2, 3)
    circuit.add_gate(ccx)
    ##CCX1,2, 0
    ccx = TOFFOLI(1,2, 0)
    circuit.add_gate(ccx)
    ##CCX1,3, 0
    ccx = TOFFOLI(1,3, 0)
    circuit.add_gate(ccx)
    ##CCX1,3, 2
    ccx = TOFFOLI(1,3, 2)
    circuit.add_gate(ccx)
    ##SWAP0,1
    circuit.add_SWAP_gate(0,1)
    ##SWAP0,2
    circuit.add_SWAP_gate(0,2)
    ##SWAP1,3
    circuit.add_SWAP_gate(1,3)
    draw_circuit(circuit, verbose=1)

    ## Example5
    circuit = QuantumCircuit(5)
    ## 3-qubit gate applied to [0,1,2]
    mat = np.identity(2**3)
    circuit.add_dense_matrix_gate([0,1,2], mat)
    ## 3-qubit gate applied to [0,3,4], and [1] qubit is control-qubit
    c_dense_gate = DenseMatrix([0,3,4], mat)
    control_index = 1
    control_with_value = 1
    c_dense_gate.add_control_qubit(control_index, control_with_value)
    circuit.add_gate(c_dense_gate)
    ## 3-qubit gate applied to [0,2,4]
    circuit.add_dense_matrix_gate([0,2,4], mat)
    ## SWAP gate aplied to [1,3], and [2] qubit is control-qubit
    swp_gate = to_matrix_gate(SWAP(1,3))
    control_index = 2
    control_with_value = 1
    swp_gate.add_control_qubit(control_index, control_with_value)
    circuit.add_gate(swp_gate)
    draw_circuit(circuit)