def get_bare_stabilizer(H: QubitOperator): ''' Identify the stabilizer of H. Currently admits only stabilizer with all z since hf can only identifies the value of these terms ''' n = get_number_qubit(H) pws = [] for pw, _ in H.terms.items(): pws.append(pw) binvecs = pauli2binvec(pws, n) nullvecs = binary_null_space(np.array(binvecs)) stabs = [] for vec in nullvecs: # If is all z if all(vec[:n] == 0): stab = QubitOperator.identity() for i in range(n): if vec[n + i] == 1: stab = stab * QubitOperator('Z' + str(i)) stabs.append(stab) else: print('Stabilizer with x/y terms ignored. ') return stabs
def paulistrings(self, other): """ Reassign with Tequila PauliString format :param other: list of PauliStrings :return: self for chaining """ new_hamiltonian = QubitOperator.identity() for ps in other: tmp = QubitOperator(term=ps.key_openfermion(), value=ps.coeff) new_hamiltonian += tmp self._qubit_operator = new_hamiltonian return self
def get_zform_unitary(H_qwc: QubitOperator): ''' Get the unitary that transforms qwc operators to all-z form. ''' qwc_ops = {} # dictionary of qub : x/y/z for pw, _ in H_qwc.terms.items(): for ps in pw: qwc_ops[ps[0]] = ps[1] U = QubitOperator.identity() for qub, op in qwc_ops.items(): if op != 'Z': U *= 1 / 2**(1 / 2) * (QubitOperator(term=op + str(qub)) + QubitOperator(term='Z' + str(qub))) return U
def get_qwc_unitary(H: QubitOperator): ''' Get the unitary that transform commuting operators to qwc operators ''' qh = QubitHamiltonian.from_openfermion(H) bh = BinaryHamiltonian.init_from_qubit_hamiltonian(qh) qwc, lag, sig = bh.single_qubit_form() num = len(lag) U = QubitOperator.identity() for idx in range(num): l = QubitHamiltonian.from_paulistrings(lag[idx].to_pauli_strings()) s = QubitHamiltonian.from_paulistrings(sig[idx].to_pauli_strings()) U *= 1 / 2**(1 / 2) * (l.to_openfermion() + s.to_openfermion()) return U
def __init__(self, qubit_hamiltonian: typing.Union[QubitOperator, str, numbers.Number] = None): """ Initialize from string or from a preexisting OpenFermion QubitOperator instance :param qubit_hamiltonian: string or openfermion.QubitOperator if string: Same conventions as openfermion if None: The Hamiltonian is initialized as identity operator if Number: initialized as scaled unit operator """ if isinstance(qubit_hamiltonian, str): self._qubit_operator = self.from_string(string=qubit_hamiltonian)._qubit_operator elif qubit_hamiltonian is None: self._qubit_operator = QubitOperator.zero() elif isinstance(qubit_hamiltonian, numbers.Number): self._qubit_operator = qubit_hamiltonian * QubitOperator.identity() else: self._qubit_operator = qubit_hamiltonian assert (isinstance(self._qubit_operator, QubitOperator))
def unit(cls): return QubitHamiltonian(qubit_operator=QubitOperator.identity())