unitaryfund / mitiq

Mitiq is an open source toolkit for implementing error mitigation techniques on most current intermediate-scale quantum computers.
https://mitiq.readthedocs.io
GNU General Public License v3.0
363 stars 163 forks source link

Mitiq latest version and integration with qiskit issue #765

Closed martin-rv closed 3 years ago

martin-rv commented 3 years ago

Dear Mitiq team,

I am trying to use mitiq to perform error mitigation in my calculation. With my installation, I can successfully run the code for cirq in the "Getting Started" section.

https://mitiq.readthedocs.io/en/stable/readme.html

However, when I try the Qiskit example in

https://mitiq.readthedocs.io/en/stable/examples/ibmq-backends.html

I get errors (see below) that go beyond my experience.

Any help would be appreciated.

Best, Martin

`import qiskit import mitiq from mitiq.zne import execute_with_zne

qreg, creg = qiskit.QuantumRegister(1), qiskit.ClassicalRegister(1) circuit = qiskit.QuantumCircuit(qreg, creg) for _ in range(10): circuit.x(qreg) circuit.measure(qreg, creg)

if qiskit.IBMQ.stored_account(): provider = qiskit.IBMQ.load_account() backend = provider.get_backend("ibmq_qasm_simulator") # Set quantum computer here! else:

Default to a simulator.

backend = qiskit.BasicAer.backends()[0]

def ibmq_executor(circuit: qiskit.QuantumCircuit, shots: int = 1024) -> float: """Returns the expectation value to be mitigated.

Args:
    circuit: Circuit to run.
    shots: Number of times to execute the circuit to compute the expectation value.
"""
# Run the circuit
job = qiskit.execute(
    experiments=circuit,
    backend=backend,
    optimization_level=0,  # Important to preserve folded gates.
    shots=shots
)

# Convert from raw measurement counts to the expectation value
counts = job.result().get_counts()
if counts.get("0") is None:
    expectation_value = 0.
else:
    expectation_value = counts.get("0") / shots
return expectation_value

mitigated = mitiq.zne.execute_with_zne(circuit, ibmq_executor)`

I get the error

`CircuitError Traceback (most recent call last)

in 39 return expectation_value 40 ---> 41 mitigated = mitiq.zne.execute_with_zne(circuit, ibmq_executor) ~\anaconda3\lib\site-packages\mitiq\zne\zne.py in execute_with_zne(qp, executor, factory, scale_noise, num_to_average) 60 raise ValueError("Argument `num_to_average` must be a positive int.") 61 ---> 62 return factory.run(qp, executor, scale_noise, int(num_to_average)).reduce() 63 64 ~\anaconda3\lib\site-packages\mitiq\zne\inference.py in run(self, qp, executor, scale_noise, num_to_average) 520 521 # Get all noise-scaled circuits to run --> 522 to_run = self._generate_circuits(qp, scale_noise, num_to_average) 523 524 if len(qp) < 5: ~\anaconda3\lib\site-packages\mitiq\zne\inference.py in _generate_circuits(self, circuit, scale_noise, num_to_average) 589 for scale_factor in self.get_scale_factors(): 590 for _ in range(num_to_average): --> 591 to_run.append(scale_noise(circuit, scale_factor)) 592 return to_run 593 ~\anaconda3\lib\site-packages\mitiq\interface\conversions.py in new_scaling_function(circuit, *args, **kwargs) 231 232 scaled_circuit.remove_final_measurements() --> 233 _transform_registers( 234 scaled_circuit, new_qregs=circuit.qregs, 235 ) ~\anaconda3\lib\site-packages\mitiq\interface\mitiq_qiskit\conversions.py in _transform_registers(circuit, new_qregs) 196 new_ops.append((gate, new_qubits, cbits)) 197 --> 198 circuit.data = new_ops 199 200 ~\anaconda3\lib\site-packages\qiskit\circuit\quantumcircuit.py in data(self, data_input) 257 258 for inst, qargs, cargs in data_input: --> 259 self.append(inst, qargs, cargs) 260 261 @property ~\anaconda3\lib\site-packages\qiskit\circuit\quantumcircuit.py in append(self, instruction, qargs, cargs) 1011 instructions = InstructionSet() 1012 for (qarg, carg) in instruction.broadcast_arguments(expanded_qargs, expanded_cargs): -> 1013 instructions.add(self._append(instruction, qarg, carg), qarg, carg) 1014 return instructions 1015 ~\anaconda3\lib\site-packages\qiskit\circuit\quantumcircuit.py in _append(self, instruction, qargs, cargs) 1035 # do some compatibility checks 1036 self._check_dups(qargs) -> 1037 self._check_qargs(qargs) 1038 self._check_cargs(cargs) 1039 ~\anaconda3\lib\site-packages\qiskit\circuit\quantumcircuit.py in _check_qargs(self, qargs) 1156 raise CircuitError("qarg is not a Qubit") 1157 if not set(qargs).issubset(self._qubit_set): -> 1158 raise CircuitError("qargs not in this circuit") 1159 1160 def _check_cargs(self, cargs): CircuitError: 'qargs not in this circuit' ` Thank you, Martin
github-actions[bot] commented 3 years ago

Hello @martin-rv, thank you for your interest in Mitiq! If this is a bug report, please provide screenshots and/or minimum viable code to reproduce your issue, so we can do our best to help get it fixed. If you have any questions in the meantime, you can also ask us on the Unitary Fund Discord.

martin-rv commented 3 years ago

About my installation:

Mitiq: A Python toolkit for implementing error mitigation on quantum computers

Authored by: Mitiq team, 2020 & later (https://github.com/unitaryfund/mitiq)

Mitiq Version: 0.9.1

Core Dependencies

Cirq Version: 0.10.0 NumPy Version: 1.20.1 SciPy Version: 1.4.1

Optional Dependencies

PyQuil Version: Not installed Qiskit Version: 0.27.0 Braket Version: Not installed

Python Version: 3.8.8 Platform Info: Windows (AMD64)

rmlarose commented 3 years ago

Thanks @martin-rv, I confirmed this behavior. It is due to your Qiskit version - currently we aren't compatible with Qiskit 0.27.0.

I just checked the example works when you pip install qiskit==0.24.0. You can use this Qiskit version with Mitiq in the meantime until we decide a longer-term action for this issue.

Thanks again for reporting this and please let us know if you run into further issues.

martin-rv commented 3 years ago

Thanks for your quick reply, @rmlarose! I installed Qiskit version 0.24 as you recommended, and the example works now.

I am planning to use Mitiq for my research (we are modeling materials with quantum computers), so I look forward to integration with the latest Qiskit version.