OpenFermion-Cirq icon indicating copy to clipboard operation
OpenFermion-Cirq copied to clipboard

QubitOperator to cirq.PauliSum

Open mpharrigan opened this issue 5 years ago • 3 comments

We need a simple function to convert OpenFermion's QubitOperator to the corresponding Cirq object, PauliSum. Here's a snippet that does it:

def qubit_operator_to_pauli_sum(qubit_op):
    psum = cirq.PauliSum()    
    for ind_ops, coeff in qubit_op.terms.items():
        if ind_ops == tuple():
            psum += coeff
            continue
        pstring = cirq.PauliString()    
        for ind, op in ind_ops:
            if op == 'X':
                op = cirq.X
            elif op == 'Y':
                op = cirq.Y
            elif op == 'Z':
                op = cirq.Z
                
            pstring *= op(cirq.LineQubit(ind))
        psum += pstring * coeff
            
    return psum

but it needs tests and stuff

See also https://github.com/quantumlib/OpenFermion-Cirq/issues/368#issuecomment-584381039

mpharrigan avatar Mar 05 '20 18:03 mpharrigan

Hi @mpharrigan ,

We could align this with my PR #395 . Do you mind if I take this function and we start a set of translator/serializers OpenFermion to OpenFermion-Cirq?

Best, Xavi

xabomon avatar Apr 21 '20 19:04 xabomon

Please do! it might make reviewing easier if you keep some of the translators (like above) in a separate, small, easy-to-review PR that we can get in tout suite

mpharrigan avatar Apr 22 '20 00:04 mpharrigan

Yes sure! I will make a new PR for this indeed. Thanks!

xabomon avatar Apr 22 '20 06:04 xabomon