|
| 1 | +# Copyright 2020 ProjectQ-Framework (www.projectq.ch) |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for projectq.libs.qasm._utils.py.""" |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from projectq.types import WeakQubitRef |
| 19 | +from projectq.cengines import DummyEngine |
| 20 | +from projectq.ops import (X, Y, Z, T, Tdagger, S, Sdagger, H, Ph, R, Rx, Ry, Rz, |
| 21 | + U2, U3, Swap, Toffoli, Barrier, C) |
| 22 | +from ._utils import apply_gate, OpaqueGate |
| 23 | + |
| 24 | +# ============================================================================== |
| 25 | + |
| 26 | + |
| 27 | +def test_opaque_gate(): |
| 28 | + gate = OpaqueGate('my_gate', None) |
| 29 | + assert gate.name == 'my_gate' |
| 30 | + assert not gate.params |
| 31 | + assert str(gate) == 'Opaque(my_gate)' |
| 32 | + |
| 33 | + gate = OpaqueGate('my_gate', ('lambda', 'alpha')) |
| 34 | + assert gate.name == 'my_gate' |
| 35 | + assert gate.params == ('lambda', 'alpha') |
| 36 | + |
| 37 | + assert str(gate) == 'Opaque(my_gate)(lambda,alpha)' |
| 38 | + |
| 39 | + |
| 40 | +# ============================================================================== |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize( |
| 44 | + 'gate, n_qubits', (list( |
| 45 | + map(lambda x: |
| 46 | + (x, 1), [ |
| 47 | + X, Y, Z, S, Sdagger, T, Tdagger, H, Barrier, |
| 48 | + Ph(1.12), |
| 49 | + Rx(1.12), |
| 50 | + Ry(1.12), |
| 51 | + Rz(1.12), |
| 52 | + R(1.12), |
| 53 | + U2(1.12, 1.12), |
| 54 | + U3(1.12, 1.12, 1.12), |
| 55 | + ])) + list(map(lambda x: |
| 56 | + (x, 2), [C(X), C(Y), C(Z), Swap, Barrier])) + |
| 57 | + list(map(lambda x: |
| 58 | + (x, 3), [Toffoli, C(Swap), Barrier])) + |
| 59 | + list(map(lambda x: (x, 10), [Barrier]))), |
| 60 | + ids=str) |
| 61 | +def test_apply_gate(gate, n_qubits): |
| 62 | + backend = DummyEngine() |
| 63 | + backend.is_last_engine = True |
| 64 | + |
| 65 | + gate.engine = backend |
| 66 | + qubits = [WeakQubitRef(backend, idx) for idx in range(n_qubits)] |
| 67 | + |
| 68 | + apply_gate(gate, qubits) |
0 commit comments