QuantumComputer.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:QuantumComputing 作者: corbett 项目源码 文件源码
def apply_two_qubit_gate_CNOT(self,first_qubit_name,second_qubit_name):
        """ Should work for all combination of qubits"""
        first_qubit=self.qubits.get_quantum_register_containing(first_qubit_name)
        second_qubit=self.qubits.get_quantum_register_containing(second_qubit_name)
        if len(first_qubit.get_noop())>0 or len(second_qubit.get_noop())>0:
            raise Exception("Control or target qubit has been measured previously, no more gates allowed")
        if not first_qubit.is_entangled() and not second_qubit.is_entangled():
            combined_state=np.kron(first_qubit.get_state(),second_qubit.get_state())
            if first_qubit.get_num_qubits()!=1 or second_qubit.get_num_qubits()!=1:
                raise Exception("Both qubits are marked as not entangled but one or the other has an entangled state")
            new_state=Gate.CNOT2_01*combined_state
            if State.is_fully_separable(new_state):
                second_qubit.set_state(State.get_second_qubit(new_state))
            else:
                self.qubits.entangle_quantum_registers(first_qubit,second_qubit)
                first_qubit.set_state(new_state)
        else:
            if not first_qubit.is_entangled_with(second_qubit):
                # Entangle the state
                combined_state=np.kron(first_qubit.get_state(),second_qubit.get_state())
                self.qubits.entangle_quantum_registers(first_qubit,second_qubit)
            else:
                # We are ready to do the operation
                combined_state=first_qubit.get_state()
            # Time for more meta programming!
            # Select gate based on indices
            control_qubit_idx,target_qubit_idx=first_qubit.get_indices(second_qubit)
            gate_size=QuantumRegister.num_qubits(combined_state)
            try:
                exec 'gate=Gate.CNOT%d_%d%d' %(gate_size,control_qubit_idx,target_qubit_idx) 
            except:
                print 'gate=Gate.CNOT%d_%d%d' %(gate_size,control_qubit_idx,target_qubit_idx)
                raise Exception("Unrecognized combination of number of qubits")
            first_qubit.set_state(gate*combined_state)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号