def create_from_pure_vector(pure_vector, state_type, basis='pp', evotype='default', state_space=None, on_construction_error='warn'): """ TODO: docstring -- create a State from a state vector """ state_type_preferences = (state_type, ) if isinstance(state_type, str) else state_type if state_space is None: state_space = _statespace.default_space_for_udim(len(pure_vector)) for typ in state_type_preferences: try: if typ == 'computational': st = ComputationalBasisState.from_pure_vector( pure_vector, basis, evotype, state_space) #elif typ == ('static stabilizer', 'static clifford'): # st = StaticStabilizerState(...) # TODO elif typ == 'static pure': st = StaticPureState(pure_vector, basis, evotype, state_space) elif typ == 'full pure': st = FullPureState(pure_vector, basis, evotype, state_space) elif typ in ('static', 'full', 'full TP', 'TrueCPTP'): superket = _bt.change_basis(_ot.state_to_dmvec(pure_vector), 'std', basis) st = create_from_dmvec(superket, typ, basis, evotype, state_space) elif _ot.is_valid_lindblad_paramtype(typ): from ..operations import LindbladErrorgen as _LindbladErrorgen, ExpErrorgenOp as _ExpErrorgenOp static_state = create_from_pure_vector( pure_vector, ('computational', 'static pure'), basis, evotype, state_space) proj_basis = 'PP' if state_space.is_entirely_qubits else basis errorgen = _LindbladErrorgen.from_error_generator( state_space.dim, typ, proj_basis, basis, truncate=True, evotype=evotype, state_space=state_space) st = ComposedState(static_state, _ExpErrorgenOp(errorgen)) else: raise ValueError("Unknown state type '%s'!" % str(typ)) return st # if we get to here, then we've successfully created a state to return except (ValueError, AssertionError) as err: if on_construction_error == 'raise': raise err elif on_construction_error == 'warn': print( 'Failed to construct state with type "{}" with error: {}'. format(typ, str(err))) pass # move on to next type raise ValueError( "Could not create a state of type(s) %s from the given pure vector!" % (str(state_type)))
def __init__(self, purevec, basis, evotype, state_space): purevec = _State._to_vector(purevec) purevec = purevec.astype(complex) state_space = _statespace.default_space_for_udim(purevec.shape[0]) if (state_space is None) \ else _statespace.StateSpace.cast(state_space) evotype = _Evotype.cast(evotype) basis = _Basis.cast( basis, state_space.dim) # basis for Hilbert-Schmidt (superop) space #Try to create a dense pure rep. If this fails, see if a dense superkey rep # can be created, as this type of rep can also hold arbitrary pure states. try: rep = evotype.create_pure_state_rep(purevec, basis, state_space) self._reptype = 'pure' self._purevec = self._basis = None except Exception: if len(purevec) == basis.dim and _np.linalg.norm( purevec.imag) < 1e-10: # Special case when a *superket* was provided instead of a purevec superket_vec = purevec.real # used as a convenience case that really shouldn't be used else: superket_vec = _bt.change_basis(_ot.state_to_dmvec(purevec), 'std', basis) rep = evotype.create_dense_state_rep(superket_vec, state_space) self._reptype = 'superket' self._purevec = purevec self._basis = basis _State.__init__(self, rep, evotype) DenseStateInterface.__init__(self)
def __init__(self, mx, basis, evotype, state_space): """ Initialize a new LinearOperator """ mx = _LinearOperator.convert_to_matrix(mx) state_space = _statespace.default_space_for_udim(mx.shape[0]) if (state_space is None) \ else _statespace.StateSpace.cast(state_space) basis = _Basis.cast( basis, state_space.dim) # basis for Hilbert-Schmidt (superop) space evotype = _Evotype.cast(evotype) #Try to create a dense unitary rep. If this fails, see if a dense superop rep # can be created, as this type of rep can also hold arbitrary unitary ops. try: rep = evotype.create_dense_unitary_rep(mx, basis, state_space) self._reptype = 'unitary' self._unitary = None except Exception: if mx.shape[0] == basis.dim and _np.linalg.norm(mx.imag) < 1e-10: # Special case when a *superop* was provided instead of a unitary mx superop_mx = mx.real # used as a convenience case that really shouldn't be used else: superop_mx = _ot.unitary_to_superop(mx, basis) rep = evotype.create_dense_superop_rep(superop_mx, state_space) self._reptype = 'superop' self._unitary = mx self._basis = basis _LinearOperator.__init__(self, rep, evotype) DenseOperatorInterface.__init__(self)
def create_from_unitary_mx(unitary_mx, op_type, basis='pp', stdname=None, evotype='default', state_space=None): """ TODO: docstring - note that op_type can be a list/tuple of types in order of precedence """ op_type_preferences = verbose_type_from_op_type(op_type) error_msgs = {} U = unitary_mx if state_space is None: state_space = _statespace.default_space_for_udim(U.shape[0]) for typ in op_type_preferences: try: if typ == 'static standard' and stdname is not None: op = StaticStandardOp(stdname, basis, evotype, state_space) elif typ == 'static clifford': op = StaticCliffordOp(U, None, basis, evotype, state_space) elif typ == 'static unitary': op = StaticUnitaryOp(U, basis, evotype, state_space) elif typ == "full unitary": op = FullUnitaryOp(U, basis, evotype, state_space) elif typ in ('static', 'full', 'full TP', 'linear'): superop_mx = _ot.unitary_to_superop(U, basis) op = create_from_superop_mx(superop_mx, op_type, basis, stdname, evotype, state_space) elif _ot.is_valid_lindblad_paramtype(typ): # maybe "lindblad XXX" where XXX is a valid lindblad type? if _np.allclose(U, _np.identity(U.shape[0], 'd')): unitary_postfactor = None else: unitary_postfactor = create_from_unitary_mx( U, ('static standard', 'static clifford', 'static unitary'), basis, stdname, evotype, state_space) proj_basis = 'PP' if state_space.is_entirely_qubits else basis errorgen = LindbladErrorgen.from_error_generator(state_space.dim, typ, proj_basis, basis, truncate=True, evotype=evotype, state_space=state_space) op = ExpErrorgenOp(errorgen) if (unitary_postfactor is None) \ else ComposedOp([unitary_postfactor, ExpErrorgenOp(errorgen)]) if op.dim <= 16: # only do this for up to 2Q operations, otherwise to_dense is too expensive expected_superop_mx = _ot.unitary_to_superop(U, basis) assert (_np.linalg.norm(op.to_dense('HilbertSchmidt') - expected_superop_mx) < 1e-6), \ "Failure to create Lindblad operation (maybe due the complex log's branch cut?)" else: raise ValueError("Unknown operation type '%s'!" % str(typ)) return op # if we get to here, then we've successfully created an op to return except (ValueError, AssertionError, AttributeError) as e: #_warnings.warn('Failed to create operator with type %s with error: %s' % (typ, e)) error_msgs[typ] = str(e) # # move on to text type raise ValueError("Could not create an operator of type(s) %s from the given unitary op:\n%s" % (str(op_type), str(error_msgs)))
def create_from_pure_vectors(pure_vectors, povm_type, basis='pp', evotype='default', state_space=None, on_construction_error='warn'): """ TODO: docstring -- create a POVM from a list/dict of (key, pure-vector) pairs """ povm_type_preferences = (povm_type,) if isinstance(povm_type, str) else povm_type if not isinstance(pure_vectors, dict): # then assume it's a list of (key, value) pairs pure_vectors = _collections.OrderedDict(pure_vectors) if state_space is None: state_space = _statespace.default_space_for_udim(len(next(iter(pure_vectors.values())))) for typ in povm_type_preferences: try: if typ == 'computational': povm = ComputationalBasisPOVM.from_pure_vectors(pure_vectors, evotype, state_space) #elif typ in ('static stabilizer', 'static clifford'): # povm = ComputationalBasisPOVM(...evotype='stabilizer') ?? elif typ in ('static pure', 'full pure', 'static', 'full'): effects = [(lbl, create_effect_from_pure_vector(vec, typ, basis, evotype, state_space)) for lbl, vec in pure_vectors.items()] povm = UnconstrainedPOVM(effects, evotype, state_space) elif typ == 'full TP': effects = [(lbl, create_effect_from_pure_vector(vec, "full", basis, evotype, state_space)) for lbl, vec in pure_vectors.items()] povm = TPPOVM(effects, evotype, state_space) elif _ot.is_valid_lindblad_paramtype(typ): from ..operations import LindbladErrorgen as _LindbladErrorgen, ExpErrorgenOp as _ExpErrorgenOp base_povm = create_from_pure_vectors(pure_vectors, ('computational', 'static pure'), basis, evotype, state_space) proj_basis = 'PP' if state_space.is_entirely_qubits else basis errorgen = _LindbladErrorgen.from_error_generator(state_space.dim, typ, proj_basis, basis, truncate=True, evotype=evotype, state_space=state_space) povm = ComposedPOVM(_ExpErrorgenOp(errorgen), base_povm, mx_basis=basis) else: raise ValueError("Unknown POVM type '%s'!" % str(typ)) return povm # if we get to here, then we've successfully created a state to return except (ValueError, AssertionError) as err: if on_construction_error == 'raise': raise err elif on_construction_error == 'warn': print('Failed to construct povm with type "{}" with error: {}'.format(typ, str(err))) pass # move on to next type raise ValueError("Could not create a POVM of type(s) %s from the given pure vectors!" % (str(povm_type)))
def __init__(self, unitary, symplecticrep=None, basis='pp', evotype='default', state_space=None): self.unitary = unitary assert (self.unitary is not None), "Must supply `unitary` argument!" U = self.unitary.to_dense() if isinstance( self.unitary, _LinearOperator) else self.unitary # Make contiguous for Cython-based evotypes U = _np.ascontiguousarray(U, dtype=complex) state_space = _statespace.default_space_for_udim(U.shape[0]) if (state_space is None) \ else _statespace.StateSpace.cast(state_space) evotype = _Evotype.cast(evotype) rep = evotype.create_clifford_rep(U, symplecticrep, basis, state_space) _LinearOperator.__init__(self, rep, evotype)