def separate_state(qubit_state):
"""This only works if the state is fully separable at present
Throws exception if not a separable state"""
n_entangled=QuantumRegister.num_qubits(qubit_state)
if list(qubit_state.flat).count(1)==1:
separated_state=[]
idx_state=list(qubit_state.flat).index(1)
add_factor=0
size=qubit_state.shape[0]
while(len(separated_state)<n_entangled):
size=size/2
if idx_state<(add_factor+size):
separated_state+=[State.zero_state]
add_factor+=0
else:
separated_state+=[State.one_state]
add_factor+=size
return separated_state
else:
# Try a few naive separations before giving up
cardinal_states=[State.zero_state,State.one_state,State.plus_state,State.minus_state,State.plusi_state,State.minusi_state]
for separated_state in itertools.product(cardinal_states, repeat=n_entangled):
candidate_state=reduce(lambda x,y:np.kron(x,y),separated_state)
if np.allclose(candidate_state,qubit_state):
return separated_state
# TODO: more general separation methods
raise StateNotSeparableException("TODO: Entangled qubits not represented yet in quantum computer implementation. Can currently do manual calculations; see TestBellState for Examples")
评论列表
文章目录