From 5ff555fba6af90e3fa0f92f5694623f300a1d73e Mon Sep 17 00:00:00 2001 From: Pradnya Khalate Date: Thu, 5 Dec 2024 14:26:45 -0800 Subject: [PATCH] * Disallow passing qvector(s) arguments to custom operations. Signed-off-by: Pradnya Khalate --- python/cudaq/kernel/ast_bridge.py | 12 +++++- python/tests/custom/test_custom_operations.py | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/python/cudaq/kernel/ast_bridge.py b/python/cudaq/kernel/ast_bridge.py index 5f566768c2..d76a802d72 100644 --- a/python/cudaq/kernel/ast_bridge.py +++ b/python/cudaq/kernel/ast_bridge.py @@ -1826,7 +1826,11 @@ def bodyBuilder(iterVal): targets = [self.popValue() for _ in range(numTargets)] targets.reverse() - self.checkControlAndTargetTypes([], targets) + for i, t in enumerate(targets): + if not quake.RefType.isinstance(t.type): + self.emitFatalError( + f'invalid target operand {i}, broadcasting is not supported on custom operations.' + ) globalName = f'{nvqppPrefix}{node.func.id}_generator_{numTargets}.rodata' @@ -2678,6 +2682,12 @@ def bodyBuilder(iterVal): targets = [self.popValue() for _ in range(numTargets)] targets.reverse() + for i, t in enumerate(targets): + if not quake.RefType.isinstance(t.type): + self.emitFatalError( + f'invalid target operand {i}, broadcasting is not supported on custom operations.' + ) + globalName = f'{nvqppPrefix}{node.func.value.id}_generator_{numTargets}.rodata' currentST = SymbolTable(self.module.operation) diff --git a/python/tests/custom/test_custom_operations.py b/python/tests/custom/test_custom_operations.py index ed2f3cfa49..e1772b2ebe 100644 --- a/python/tests/custom/test_custom_operations.py +++ b/python/tests/custom/test_custom_operations.py @@ -230,6 +230,45 @@ def bell(): error) +def test_bug_2452(): + cudaq.register_operation("custom_i", np.array([1, 0, 0, 1])) + + @cudaq.kernel + def kernel1(): + qubits = cudaq.qvector(2) + custom_i(qubits) + + with pytest.raises(RuntimeError) as error: + kernel1.compile() + assert 'broadcasting is not supported on custom operations' in repr(error) + + cudaq.register_operation("custom_x", np.array([0, 1, 1, 0])) + + @cudaq.kernel + def kernel2(): + qubit = cudaq.qubit() + ancilla = cudaq.qvector(2) + x(ancilla) + custom_x.ctrl(ancilla, qubit) # `controls` can be `qvector` + + counts = cudaq.sample(kernel2) + assert len(counts) == 1 and '111' in counts + + cudaq.register_operation( + "custom_cz", np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, + -1])) + + @cudaq.kernel + def kernel3(): + qubits = cudaq.qvector(2) + custom_cz(qubits) + + with pytest.raises(RuntimeError) as error: + cudaq.sample(kernel3) + assert 'invalid number of arguments (1) passed to custom_cz (requires 2 arguments)' in repr( + error) + + # leave for gdb debugging if __name__ == "__main__": loc = os.path.abspath(__file__)