-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support alternative tensor product ordering #7628
Comments
I think the right way to go about this is to add a Qiskit (Terra) internally reliably uses the little-endian convention across the board when crossing API boundaries (like IQX -> Terra, etc), and I don't think it's a good idea to blur that (e.g. I'm not 100% convinced that it's a good idea to add arguments to any |
@jakelishman Permuting quantum objects is a good feature, but it is a separate issue. Permuting a quantum object is a physical operation (i.e. permuting qubits is equivalent to performing a SWAP gate on them). This issue is about requesting information about an object, but in a different convention than the one Qiskit uses by default. Of course, this can be simulated by permuting the objects, but that is only a workaround/hack. |
I see this issue is raising its head again. It was decided several years ago that Qiskit would use the little-endian convention everywhere as it makes quite a bit of sense. I am not sure that this issue will gain much traction given that all agreed that this was the way forward many years ago, and mixing conventions is never a good thing. |
I understand the concern about mixing conventions, but I don't think it's such a big deal. I'm not suggesting mixing conventions anywhere within the library, but only providing the option to the end user. Besides, the Qiskit "ecosystem" has already started mixing conventions, considering that both Qiskit Nature and mthree use big-endian. |
M3 uses the standard Qiskit convention. Qiskit Nature should as well, save for perhaps some unintentional usage |
import mthree
from qiskit import QuantumCircuit
from qiskit.providers.aer import AerSimulator
backend = AerSimulator(method='statevector')
mit = mthree.M3Mitigation(backend)
mit.cals_from_system(range(2))
circuit = QuantumCircuit(2)
circuit.x(0)
circuit.measure_all()
counts = backend.run(circuit, shots=100).result().get_counts()
quasis = mit.apply_correction(counts, range(2))
print(f"Expectation of 01: {quasis.expval('01')}")
print(f"Expectation of 10: {quasis.expval('10')}") prints
The developers of Qiskit Nature deliberately chose to use big-endian instead of little-endian throughout the library (as @mrossinek can confirm). When they convert Nature operators to Terra operators, a silent switch to little-endian is performed. |
I'm happy to accept a small, self-contained and composable function If you want the returned results to be in a different order when you retrieve them, you can already build the initial quantum circuit using your preferred convention, which doesn't involve any physical operations. You can easily do this without any reference to bit numbers by using the exact from qiskit.circuit import QuantumCircuit, Qubit, Clbit
my_qubits = [Qubit() for _ in [None]*10]
my_clbits = [Clbit() for _ in [None]*10]
my_qlayout = [my_qubits[x] for x in <whatever layout>]
my_clayout = [my_clbits[x] for x in <whatever layout>]
circuit = QuantumCircuit(my_qlayout, my_clayout)
circuit.h(my_qubits[0])
circuit.cx(my_qubits[0], my_qubits[1])
circuit.measure(my_qubits[1], my_clbits[1]) (In fact, internally this is actually the most efficient way to construct a circuit, though the difference is marginal.) The hypothetical For those reasons, I'm actually not a fan of |
@jakelishman I tried to simulate the circuit in your code using AerSimulator but got
By "physical operation" I did not mean that it is related to hardware. I meant that it is a physically significant operation. For example, suppose the operator represents the Hamiltonian of a particular physical system being modeled. Enacting a non-trivial permutation on the operator would result in a different operator that does not necessarily model the same physical system any more. |
Oh, that's pretty weird about the At any rate, if you put the bits in the circuit with my_qlayout = QuantumRegister(bits=[my_qubits[x] for x in ...])
my_clayout = ClassicalRegister(bits=[my_clbits[x] for x in ...])
circuit = QuantumCircuit(my_qlayout, my_clayout) it should be fine again (you can still use Sure, but you can do all your calculations in a "wrong" convention as long as you're consistent, then just permute them into the "right" convention whenever you want to look at the results. Nothing in there changes the physical meaning. In circuits where the layout demands physical swap gates be inserted, Terra's transpiler will actually do this logical permutation already by mutating any final |
Just to mention it again in case you missed it: it's actually already possible in |
Thanks. As I said above, I am indeed aware that permuting objects can be used to achieve the same effect as reordering labels. But again, this is just a workaround/hack. |
If the existing forms can be used for the post-processing you'd like, I'll close this issue, but if you'd like to mentor a project to expand |
What should we add?
Qiskit uses the little endian convention for ordering the factors in a tensor product. For example, the statevector for a 3-qubit state would use the ordering
Q2 ⊗ Q1 ⊗ Q0
. It would be useful to add support for specifying the tensor product order. This could be used, for example, to request a state vector using the common big endian ordering.This can be implemented by adding a
qubit_order
argument to all functions that implicitly rely on a choice of tensor product ordering. This argument would be a list of the qubit indices in the desired tensor product order. So for the example above, it would be[2, 1, 0]
to get the state in big-endian order.The following is a non-exhaustive list of functions that would be affected:
Result.get_statevector
Result.get_counts
Operator
SparsePauliOp
Statevector
quantum_info
moduleThe text was updated successfully, but these errors were encountered: