TransWikia.com

qiskit: How to get an operator acting on a certain qubit?

Quantum Computing Asked on December 20, 2020

I have a system of $N$ qubits and want to construct a quantum operator $Z_i Z_j + Z_k$, where $Z_i$ denotes the Pauli-Z operator acting on the $i$th qubit. Is there any direct way in qiskit, how I could implement this?

I know that I can construct an operator by e.g. saying op = Z^Z, if I have a system of 2 qubits and want the operator being the Pauli-Z on each qubit. But I would like to tell qiskit the indices of the qubits that $Z$ should act on (such that on all the other qubits Identity is applied).

My way so far consists of constructing a Quantum Circuit and converting this to an operator by

circZZ = QuantumCircuit(N)  # circuit for Z_i Z_j
circZ = QuantumCircuit(N)  # circuit for Z_k
circZZ.z(i)
circZZ.z(j)
circZ.z(k)
opZZ = CircuitOp(circZZ)  # convert circuit to operator
opZ = CircuitOp(circZ)  # convert circuit to operator
op = opZZ + opZ

But that means I have to create quantum circuits everytime I want to get this operator. Is there any shorter and more elegant way to create such an operator?

2 Answers

The class Operator has a from_label method: https://qiskit.org/documentation/stubs/qiskit.quantum_info.Operator.html?highlight=operator%20from_label#qiskit.quantum_info.Operator.from_label

That means you could do something like this:

opZZ = Operator.from_label('ZZ')
opZ = Operator.from_label('Z')

It's possible to add opZZ and opZ into N-sized op. However, you have to call the _add by hand:

op = 0 * Operator.from_label('I' * N)  # Set the initial operator to zero
op = op._add(opZZ, qargs=[i,j])
op = op._add(opZ, qargs=[k])

Correct answer by luciano on December 20, 2020

You could use the feature that the Opflow in Aqua can take integers as tensorpower, like Z ^ 5 and then fill the blanks with identities. In a short function that could look like

from qiskit.aqua.operators import Z, I

def get_term(i, j, k, n):
    """i, j and k as in your description and n is the number of qubits."""
    zz = (I ^ i) ^ Z ^ (I ^ j - i - 1) ^ Z ^ (I ^ (n - j - i - 1))
    z = (I ^ k) ^ Z ^ (I ^ (n - k - 1))
    return zz + z

print(get_term(0, 2, 4, 5))
# 1.0 * ZIZII
# + 1.0 * IIIIZ

Note that the order of Z's is reversed to what you did with the circuits, so to get the same results you can just call

get_term(n - i - 1, n - j - 1, n - k - 1, n)

But this is just one way to get to the result, I'm sure there are many others! Your method, via circuits, is looks perfectly good to me.

Answered by Cryoris on December 20, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP