Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def cu_gate(unitary, control_qubit, target_qubit, nqubits):
''' generate a CU gate unitary'''
d=2**nqubits
cu=np.matrix(np.zeros((d,d)), dtype=complex)
extended_u=[unitary if i==target_qubit else qi.identity for i in range(nqubits)]
extended_u=reduce(np.kron, extended_u)
for bit in range(d):
name, state=vector_state(bit, nqubits)
cu+=qi.ketbra(state, state) if name[control_qubit]=='0' else qi.ketbra(extended_u*state, state)
return cu
def swap_gate(qubit_1, qubit_2, nqubits):
''' generate a SWAP gate unitary '''
d=2**nqubits
swap=np.matrix(np.zeros((d,d)), dtype=complex)
for bit in range(d):
name, state=vector_state(bit, nqubits)
name_swapped=list(name)
name_swapped[qubit_1]=name[qubit_2]
name_swapped[qubit_2]=name[qubit_1]
newstate=reduce(np.kron, map(lambda x: qi.zero if x=='0' else qi.one, name_swapped))
swap+=qi.ketbra(newstate, state)
return swap