Skip to content

Commit

Permalink
cov: adding coverage to some defined gates errors
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed Feb 26, 2024
1 parent 78e6bd5 commit 6485715
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/qibo/_openqasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion tests/test_models_circuit_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 6485715

Please sign in to comment.