q-optimize / c3

Toolset for control, calibration and characterization of physical systems
https://c3-toolset.readthedocs.io/
Apache License 2.0
66 stars 36 forks source link

C3-Qiskit interface produces results with incompatible state labels #58

Open lazyoracle opened 3 years ago

lazyoracle commented 3 years ago

Describe the bug Qiskit ordering of qubits is different from what is commonly used in Physics textbooks. C3 follows the general Physics textbook style. However when producing output through the C3-Qiskit interface, the expectation is to get results with compatible qubit ordering.

To Reproduce The following code snippet checks C3 output with Qiskit output. This fails due to qubit indexing mismatch:

    c3_qiskit = C3Provider()
    backend = c3_qiskit.get_backend('c3_qasm_perfect_simulator')
    backend.set_device_config("test/quickstart.hjson")
    qc = get_6_qubit_circuit
    job_sim = execute(qc, backend, shots=1000)
    result_sim = job_sim.result()

    # Test results with qiskit style qubit indexing
    qiskit_simulator = Aer.get_backend("qasm_simulator")
    qiskit_counts = execute(qc, qiskit_simulator, shots=1000).result().get_counts(qc)
    assert result_sim.get_counts(qc) == qiskit_counts

Expected behavior By default, the c3-qiskit interface should produce qiskit compatible labels with an option to disable this feature if required by the user.

Additional context Importantly, this change in the representation of multi-qubit states affects the way multi-qubit gates are represented in Qiskit. More details are available here and it might be useful to check if a possible fix goes beyond mere relabelling of qubit state names in the experiment result.

lazyoracle commented 3 years ago

Closed in #59

lazyoracle commented 3 years ago

The fix introduced in #59 misses edge cases. For example 011111 should be flipped to 111110 but that doesn't happen because any leading zeros are automatically removed when converting from binary to hex or int. So 011111 is treated as 11111 and it stays the same. A possible fix would be to delay the conversion to hex until the very last step, before returning the counts in the result data structure. It would then require changes in get_labels() and flip_labels() in c3_backend_utils.py