Skip to content
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

Sampler fails to run FidelityKernel even if circuits are transpiled #1519

Closed
repettovaleria opened this issue Mar 15, 2024 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@repettovaleria
Copy link

Describe the bug
I'm trying to run a quantum kernel using the ComputeUncompute class on a quantum system. I'm submitting a transpiled circuit however it fails raising the following issue on the IBM-quantum platform dashboard

Failed - Circuits do not match the target definition (non-ISA circuits). -- \n Transpile your circuits for the target before submitting a primitive query. For\n example, you can use the following code block given an IBMBackend object backend \n and circuits of type List[QuantumCircuit]:\n from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n pm = generate_preset_pass_manager(optimization_level=1, target=backend.target)\n isa_circuits = pm.run(circuits)\n Then pass isa_circuits to the Sampler or Estimator.\n -- https://ibm.biz/error_codes#1517

and the following error on the terminal:


Traceback (most recent call last):
File "/CTGlab/home/valeria/miniconda3/envs/qiskit-buona/lib/python3.10/site-packages/qiskit/algorithms/state_fidelities/compute_uncompute.py", line 161, in _run
    result = job.result()
  File "/CTGlab/home/valeria/miniconda3/envs/qiskit-buona/lib/python3.10/site-packages/qiskit_ibm_runtime/runtime_job.py", line 220, in result
    raise RuntimeJobFailureError(f"Unable to retrieve job result. {error_message}")
qiskit_ibm_runtime.exceptions.RuntimeJobFailureError: 'Unable to retrieve job result. Circuits do not match the target definition (non-ISA circuits). -- \\n        Transpile your circuits for the target before submitting a primitive query. For\\n        example, you can use the following code block given an IBMBackend object backend\\n        and circuits of type 
List[QuantumCircuit]:\\n            from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\\n            pm = generate_preset_pass_manager(optimization_level=1, target=backend.target)\\n            isa_circuits = pm.run(circuits)\\n        Then pass isa_circuits to the Sampler or Estimator.\\n         -- https://ibm.biz/error_codes#1517'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Volumes/HD2/home/valeria/Quantum-Machine-Learning-for-Expression-Data/Qkernel_test_ISA.py", line 113, in <module>
    test=fidelity._run(ft_map_t_qs,ft_map_t_qs,X_train_scaled[0],X_train_scaled[1])
  File "/CTGlab/home/valeria/miniconda3/envs/qiskit-buona/lib/python3.10/site-packages/qiskit/algorithms/state_fidelities/compute_uncompute.py", line 163, in _run
    raise AlgorithmError("Sampler job failed!") from exc
qiskit.algorithms.exceptions.AlgorithmError: 'Sampler job failed!'

This is happening only on real systems, whereas when run on simulator the code works fine.

Steps to reproduce
Here is the testing code I have run:

## Importing standard Qiskit libraries
#CHANGE Eroror mitigation
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.visualization import *
# Importing standard Qiskit libraries and configuring account
from qiskit_ibm_runtime import QiskitRuntimeService, Options
import qiskit_ibm_runtime 
#from qiskit.utils import algorithm_globals
#Load feature maps
from qiskit.circuit.library import ZZFeatureMap,ZFeatureMap
from qiskit.algorithms.state_fidelities import ComputeUncompute
from qiskit_machine_learning.kernels import FidelityQuantumKernel
#Load other libraries
import pickle
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import os
import json
import argparse



########################QUANTUM SESSION#########################################################
# Loading your IBM Quantum account(s)
print('Loading IBM Quantum account')
# Loading your IBM Quantum account(s)
service=QiskitRuntimeService(channel="ibm_quantum",token="***")
print('selecting backend')
backend = service.least_busy(operational=True, simulator=False)
#service.least_busy(simulator=False,
                             #operational=True,
                             #min_num_qubits=5)#service.get_backend(params['Backend']["backend"])
print(backend)
target = backend.target
coupling_map = target.build_coupling_map()
print('FT map instance')
#Instance FTMAP
n_qubits=4
reps=1
ft_map = ZZFeatureMap(feature_dimension=n_qubits, reps=1)

#transpile circuit
print('transpile circuit')
pm=generate_preset_pass_manager(target=backend.target,optimization_level=3,initial_layout=[0,1,2,3],seed_transpiler=42)
ft_map_t_qs = pm.run(ft_map)
######################## DATA PREPROCESSING ######################################################



#Generate sample data
X_train=np.random.rand(10,4)

scaler = MinMaxScaler(feature_range=(0, 1*np.pi))
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
   
#########################LAUNCH EXP#########################################################

#Set primitive sampler options
options = Options()
#Error mitigation level (resilience_level)
options.resilience_level = 1
#Optimization level
options.optimization_level = 3
#Number of shots
options.execution.shots = 2000
#Skip translation since the circuit is already transpiled
options.skip_transpilation= False

# Create a quantum kernel based on the transpiled feature map
#Set Primitive sampler
sampler = qiskit_ibm_runtime.Sampler(backend=backend, options=options)

#Set fidelity
fidelity = ComputeUncompute(sampler=sampler)
#run circuit using fidelity
test=fidelity._run(ft_map_t_qs,ft_map_t_qs,X_train_scaled[0],X_train_scaled[1])

Expected behavior
I expect the circuit to be able to run on the system without any problems since I'm transpiling it before submitting the job. This problem never occurred before when running other FidelityQuantumKernel(that employ the ComputeUncompute) instances before without any problems regarding the circuit, and this issue raised only after the qiskit-runtime-primitives-update

Suggested solutions
Is this possibly due to the ComputeUncompute construct the circuit by appending two circuits that are already transpiled wheras it might be more "proper" to constract the full circuit and then transpile it.

Additional Information
This issue first raised when I was using running aFidelityQuantumKernel that run the circuit through the ComputeUncompute class so to get to root of the problem I run the test I reported here using directly that.

The same code runs without error on a simulator

  • qiskit-ibm-runtime version: 0.17.0
  • Python version:3.10
  • Operating system:
Operating System: Ubuntu 20.04.5 LTS
Kernel: Linux 5.4.0-173-generic
 Architecture: x86-64

@repettovaleria repettovaleria added the bug Something isn't working label Mar 15, 2024
@ElePT
Copy link
Collaborator

ElePT commented Mar 15, 2024

Hi @repettovaleria thanks for your detailed description of the issue. As you already noted, the root cause of your failure is in the ComputeUncompute class, which doesn't always generate an ISA circuit directly (which as you also noted, became a problem with one of the latest primitives updates). The problem is known, affects several classes, and is being tracked here: qiskit-community/qiskit-algorithms#164. I'd suggest re-opening the issue in https://github.com/qiskit-community/qiskit-algorithms, as it cannot be fixed in qiskit-ibm-runtime.

@rbnfrhnr
Copy link

Hi @ElePT I noticed that the very same happens with circuits containing a reset operation. I attached the Qasm2.0 definitions for both circuits which only differ in one having a reset operation. The circuit was transpiled using generate_preset_pass_manager. Only the one not containing the reset operation works and the one containing the reset operation raises the same error as mentioned by @repettovaleria. Do they share the same underlying issue?

torino-transpiled-no-reset.txt
torino-transpiled-reset.txt

@jyu00
Copy link
Collaborator

jyu00 commented Mar 15, 2024

The reset instruction not being included in the target was a known issue and fixed in Qiskit 1.0.2.

@repettovaleria
Copy link
Author

Issue transfered to qiskit-community/qiskit-algorithms#165

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants