# How to use the qi.ketbra function in qi

## To help you get started, we’ve selected a few qi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately. peteshadbolt / qy / simulation / qi / circuit_model.py View on Github ``````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`````` peteshadbolt / qy / simulation / qi / circuit_model.py View on Github ``````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``````

## qi

LibQi Python bindings GitHub BSD-3-Clause Latest version published 8 months ago

59 / 100

## Popular Python code snippets

Find secure code to use in your application or website