From 64857150bf818f6d6a7296749481dfa83c325762 Mon Sep 17 00:00:00 2001 From: Andrea Papaluca Date: Mon, 26 Feb 2024 16:20:16 +0400 Subject: [PATCH] cov: adding coverage to some defined gates errors --- src/qibo/_openqasm.py | 10 ++++++++++ tests/test_models_circuit_qasm.py | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/qibo/_openqasm.py b/src/qibo/_openqasm.py index 2eb6a7bcff..e152914c1b 100644 --- a/src/qibo/_openqasm.py +++ b/src/qibo/_openqasm.py @@ -42,6 +42,16 @@ def get_gate(self, qubits: Union[list, tuple], args: Union[list, tuple]): Returns: :class:`qibo.gates.special.FusedGate`: the composed gate evaluated on the input qubits with the input arguments. """ + if len(self.args) != len(args): + raise_error( + ValueError, + f"Invalid `args` argument passed to the user-defined gate `{self.name}` upon construction. {args} was passed but something of the form {self.args} is expected.", + ) + elif len(self.qubits) != len(qubits): + raise_error( + ValueError, + f"Invalid `qubits` argument passed to the user-defined gate `{self.name}` upon construction. {qubits} was passed but something of the form {self.qubits} is expected.", + ) qubit_map = dict(zip(self.qubits, qubits)) args_map = dict(zip(self.args, args)) return self._construct_fused_gate(self.gates, qubits, qubit_map, args_map) diff --git a/tests/test_models_circuit_qasm.py b/tests/test_models_circuit_qasm.py index 867aec5a8e..5170b3bd01 100644 --- a/tests/test_models_circuit_qasm.py +++ b/tests/test_models_circuit_qasm.py @@ -487,6 +487,18 @@ def test_from_qasm_invalid_parametrized_gates(): with pytest.raises(parser.QASM3ParsingError): c = Circuit.from_qasm(target) + target = """OPENQASM 2.0; +include "qelib1.inc"; +qreg q[3]; +gate bob(theta,alpha) q0,q1 { h q1; cx q0,q1; rz(theta) q1; rx(alpha) q0; h q1; } +""" + + with pytest.raises(ValueError): + c = Circuit.from_qasm(f"{target}bob(0.1, 0.2, 0.3) q[1],q[0];") + + with pytest.raises(ValueError): + c = Circuit.from_qasm(f"{target}bob(0.1, 0.2) q[1],q[0],q[2];") + def test_from_qasm_gate_command(backend): target = """OPENQASM 2.0; @@ -497,7 +509,6 @@ def test_from_qasm_gate_command(backend): bob(-pi/2,pi) q[0],q[2]; alice q[1],q[0];""" c = Circuit.from_qasm(target) - print(c.draw()) def bob(theta, alpha, q0, q1): gate = gates.FusedGate(q0, q1)