@@ -71,6 +71,7 @@ def sample(kernel,
7171 or a list of such results in the case of `sample` function broadcasting.
7272 """
7373
74+ has_measurements = False
7475 has_conditionals_on_measure_result = False
7576
7677 if isinstance (kernel , PyKernelDecorator ):
@@ -85,20 +86,40 @@ def sample(kernel,
8586 if not hasattr (operation , 'name' ):
8687 continue
8788 if nvqppPrefix + kernel .name == operation .name .value :
89+ has_measurements = 'hasMeasurements' in operation .attributes
8890 has_conditionals_on_measure_result = 'qubitMeasurementFeedback' in operation .attributes
8991 break
90- elif isinstance (kernel , PyKernel ) and kernel .conditionalOnMeasure :
91- has_conditionals_on_measure_result = True
92+ elif isinstance (kernel , PyKernel ):
93+ if kernel .hasMeasurements :
94+ has_measurements = True
95+ if kernel .conditionalOnMeasure :
96+ has_conditionals_on_measure_result = True
9297
9398 if explicit_measurements :
9499 if not cudaq_runtime .supportsExplicitMeasurements ():
95100 raise RuntimeError (
96- "The sampling option `explicit_measurements` is not supported on this target. "
97- )
101+ "The sampling option `explicit_measurements` is not supported "
102+ "on this target." )
98103 if has_conditionals_on_measure_result :
99104 raise RuntimeError (
100- "The sampling option `explicit_measurements` is not supported on kernel with conditional logic on a measurement result."
101- )
105+ "The sampling option `explicit_measurements` is not supported "
106+ "on kernel with conditional logic on a measurement result." )
107+ if has_measurements :
108+ if cudaq_runtime .isQuantumDevice ():
109+ raise RuntimeError (
110+ "Kernels with explicit measurement operations cannot be used with "
111+ "`cudaq.sample` on hardware targets. Please remove all measurements "
112+ "from the kernel, qubits will be automatically measured at the end "
113+ "when sampling a kernel.\n "
114+ "Alternatively, use `cudaq.run` API, if supported on this target." )
115+ elif not explicit_measurements :
116+ print (
117+ "WARNING: Using `cudaq.sample` with a kernel that contains explicit "
118+ "measurements is deprecated and will be disallowed in a future release. "
119+ "Please remove all measurements from the kernel, qubits will be "
120+ "automatically measured at the end when sampling a kernel.\n "
121+ "Alternatively, use `cudaq.run` API which preserves individual "
122+ "measurement results." )
102123
103124 if noise_model != None :
104125 cudaq_runtime .set_noise (noise_model )
0 commit comments