• Python:实现deutsch jozsa算法(附完整源码)


    Python:实现deutsch jozsa算法

    #!/usr/bin/env python3
    
    import numpy as np
    import qiskit as q
    
    
    def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit:
       
        # This circuit has num_qubits+1 qubits: the size of the input,
        # plus one output qubit
        oracle_qc = q.QuantumCircuit(num_qubits + 1)
    
        # First, let's deal with the case in which oracle is balanced
        if case == "balanced":
            # First generate a random number that tells us which CNOTs to
            # wrap in X-gates:
            b = np.random.randint(1, 2**num_qubits)
            # Next, format 'b' as a binary string of length 'n', padded with zeros:
            b_str = format(b, f"0{num_qubits}b")
            # Next, we place the first X-gates. Each digit in our binary string
            # correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1
            # we apply an X-gate to that qubit:
            for index, bit in enumerate(b_str):
                if bit == "1":
                    oracle_qc.x(index)
            # Do the controlled-NOT gates for each qubit, using the output qubit
            # as the target:
            for index in range(num_qubits):
                oracle_qc.cx(index, num_qubits)
            # Next, place the final X-gates
            for index, bit in enumerate(b_str):
                if bit == "1":
                    oracle_qc.x(index)
    
        # Case in which oracle is constant
        if case == "constant":
            # First decide what the fixed output of the oracle will be
            # (either always 0 or always 1)
            output = np.random.randint(2)
            if output == 1:
                oracle_qc.x(num_qubits)
    
        oracle_gate = oracle_qc.to_gate()
        oracle_gate.name = "Oracle"  # To show when we display the circuit
        return oracle_gate
    
    
    def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit:
       he Oracle Circuit passed in arguments
       
        dj_circuit = q.QuantumCircuit(num_qubits + 1, num_qubits)
        # Set up the output qubit:
        dj_circuit.x(num_qubits)
        dj_circuit.h(num_qubits)
        # And set up the input register:
        for qubit in range(num_qubits):
            dj_circuit.h(qubit)
        # Let's append the oracle gate to our circuit:
        dj_circuit.append(oracle, range(num_qubits + 1))
        # Finally, perform the H-gates again and measure:
        for qubit in range(num_qubits):
            dj_circuit.h(qubit)
    
        for i in range(num_qubits):
            dj_circuit.measure(i, i)
    
        return dj_circuit
    
    
    def deutsch_jozsa(case: str, num_qubits: int) -> q.result.counts.Counts:
      
        # Use Aer's qasm_simulator
        simulator = q.Aer.get_backend("qasm_simulator")
    
        oracle_gate = dj_oracle(case, num_qubits)
        dj_circuit = dj_algorithm(oracle_gate, num_qubits)
    
        # Execute the circuit on the qasm simulator
        job = q.execute(dj_circuit, simulator, shots=1000)
    
        # Return the histogram data of the results of the experiment.
        return job.result().get_counts(dj_circuit)
    
    
    if __name__ == "__main__":
        print(f"Deutsch Jozsa - Constant Oracle: {deutsch_jozsa('constant', 3)}")
        print(f"Deutsch Jozsa - Balanced Oracle: {deutsch_jozsa('balanced', 3)}")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
  • 相关阅读:
    手机怎么实现图片转文字操作?学会这三招就够了
    AR人脸美颜特效解决方案,打造全方位美颜美妆新时代
    Faust勒索病毒数据恢复|金蝶、用友、管家婆、OA、速达、ERP等软件数据库恢复
    PyTorch笔记 - Attention Is All You Need (1)
    【TensorFlow2 之012】TF2.0 中的 TF 迁移学习
    在 LLM 架构中应用多专家模型
    电机与拖动 - 8 直流电机的电力拖动
    Error message “error:0308010C:digital envelope routines::unsupported“
    洛谷 P2408 不同子串个数(后缀数组)
    Lysozyme C (46-61) (chicken),62982-31-4
  • 原文地址:https://blog.csdn.net/it_xiangqiang/article/details/126128931