def test_permute_on_state_fn(self): """ Test if StateFns permute are consistent. """ num_qubits = 4 dim = 2**num_qubits primitive_list = [1.0 / (i + 1) for i in range(dim)] primitive_dict = { format(i, 'b').zfill(num_qubits): 1.0 / (i + 1) for i in range(dim) } dict_fn = DictStateFn(primitive=primitive_dict, is_measurement=True) vec_fn = VectorStateFn(primitive=primitive_list, is_measurement=True) # check if dict_fn and vec_fn are equivalent equivalent = np.allclose(dict_fn.to_matrix(), vec_fn.to_matrix()) self.assertTrue(equivalent) # permute indices = [2, 3, 0, 1] permute_dict = dict_fn.permute(indices) permute_vect = vec_fn.permute(indices) equivalent = np.allclose(permute_dict.to_matrix(), permute_vect.to_matrix()) self.assertTrue(equivalent)
def trace_to_density_op(state: qk.DictStateFn, trace_over: List[int]) -> qkinfo.DensityMatrix: """ Take a state comprised on n qubits and get the trace of the system over the subsystems specified by a list of indices. Makes no assumption about the separability of the traced subsystems and gives a density matrix as a result. """ input_statevector = qkinfo.Statevector(state.to_matrix()) return qkinfo.partial_trace(input_statevector, trace_over)
def trace_dict_state(state: qk.DictStateFn, trace_over: List[int]) -> qk.DictStateFn: """ Take a state comprised on n qubits and get the trace of the system over the subsystems specified by a list of indices. Assumes state is separable as a DictStateFn can only represent pure states. """ input_statevector = qkinfo.Statevector(state.to_matrix()) traced_statevector = qkinfo.partial_trace(input_statevector, trace_over).to_statevector() return qk.DictStateFn(traced_statevector.to_dict())