Exemple #1
0
    def test_add_circuit(self):
        """
        Addition of a circuit to a `QubitCircuit`
        """
        qc = QubitCircuit(6)
        qc.add_gate("CNOT", targets=[1], controls=[0])
        test_gate = Gate("SWAP", targets=[1, 4])
        qc.add_gate(test_gate)
        qc.add_gate("TOFFOLI", controls=[0, 1], targets=[2])
        qc.add_gate("SNOT", targets=[3])
        qc.add_gate(test_gate, index=[3])
        qc.add_measurement("M0", targets=[0], classical_store=[1])
        qc.add_1q_gate("RY", start=4, end=5, arg_value=1.570796)

        qc1 = QubitCircuit(6)

        qc1.add_circuit(qc)

        # Test if all gates and measurements are added
        assert len(qc1.gates) == len(qc.gates)

        for i in range(len(qc1.gates)):
            assert (qc1.gates[i].name == qc.gates[i].name)
            assert (qc1.gates[i].targets == qc.gates[i].targets)
            if (isinstance(qc1.gates[i], Gate)
                    and isinstance(qc.gates[i], Gate)):
                assert (qc1.gates[i].controls == qc.gates[i].controls)
                assert (qc1.gates[i].classical_controls ==
                        qc.gates[i].classical_controls)
            elif (isinstance(qc1.gates[i], Measurement)
                  and isinstance(qc.gates[i], Measurement)):
                assert (qc1.gates[i].classical_store ==
                        qc.gates[i].classical_store)

        # Test exception when qubit out of range
        pytest.raises(NotImplementedError, qc1.add_circuit, qc, start=4)

        qc2 = QubitCircuit(8)
        qc2.add_circuit(qc, start=2)

        # Test if all gates are added
        assert len(qc2.gates) == len(qc.gates)

        # Test if the positions are correct
        for i in range(len(qc2.gates)):
            if qc.gates[i].targets is not None:
                assert (qc2.gates[i].targets[0] == qc.gates[i].targets[0] + 2)
            if (isinstance(qc.gates[i], Gate)
                    and qc.gates[i].controls is not None):
                assert (qc2.gates[i].controls[0] == qc.gates[i].controls[0] +
                        2)
Exemple #2
0
    def test_add_circuit(self):
        """
        Addition of a circuit to a `QubitCircuit`
        """

        def customer_gate1(arg_values):
            mat = np.zeros((4, 4), dtype=np.complex128)
            mat[0, 0] = mat[1, 1] = 1.
            mat[2:4, 2:4] = gates.rx(arg_values)
            return Qobj(mat, dims=[[2, 2], [2, 2]])

        qc = QubitCircuit(6)
        qc.user_gates = {"CTRLRX": customer_gate1}

        qc = QubitCircuit(6)
        qc.add_gate("CNOT", targets=[1], controls=[0])
        test_gate = Gate("SWAP", targets=[1, 4])
        qc.add_gate(test_gate)
        qc.add_gate("TOFFOLI", controls=[0, 1], targets=[2])
        qc.add_gate("SNOT", targets=[3])
        qc.add_gate(test_gate, index=[3])
        qc.add_measurement("M0", targets=[0], classical_store=[1])
        qc.add_1q_gate("RY", start=4, end=5, arg_value=1.570796)
        qc.add_gate("CTRLRX", targets=[1, 2], arg_value=np.pi/2)

        qc1 = QubitCircuit(6)

        qc1.add_circuit(qc)

        # Test if all gates and measurements are added
        assert len(qc1.gates) == len(qc.gates)

        # Test if the definitions of user gates are added
        assert qc1.user_gates == qc.user_gates

        for i in range(len(qc1.gates)):
            assert (qc1.gates[i].name
                    == qc.gates[i].name)
            assert (qc1.gates[i].targets
                    == qc.gates[i].targets)
            if (isinstance(qc1.gates[i], Gate) and
                    isinstance(qc.gates[i], Gate)):
                assert (qc1.gates[i].controls
                        == qc.gates[i].controls)
                assert (qc1.gates[i].classical_controls
                        == qc.gates[i].classical_controls)
            elif (isinstance(qc1.gates[i], Measurement) and
                    isinstance(qc.gates[i], Measurement)):
                assert (qc1.gates[i].classical_store
                        == qc.gates[i].classical_store)

        # Test exception when qubit out of range
        pytest.raises(NotImplementedError, qc1.add_circuit, qc, start=4)

        qc2 = QubitCircuit(8)
        qc2.add_circuit(qc, start=2)

        # Test if all gates are added
        assert len(qc2.gates) == len(qc.gates)

        # Test if the positions are correct
        for i in range(len(qc2.gates)):
            if qc.gates[i].targets is not None:
                assert (qc2.gates[i].targets[0]
                        == qc.gates[i].targets[0]+2)
            if (isinstance(qc.gates[i], Gate) and
                    qc.gates[i].controls is not None):
                assert (qc2.gates[i].controls[0]
                        == qc.gates[i].controls[0]+2)

        # Test exception when the operators to be added are not gates or measurements
        qc.gates[-1] = 0
        pytest.raises(TypeError, qc2.add_circuit, qc)
Exemple #3
0
    def test_add_gate(self):
        """
        Addition of a gate object directly to a `QubitCircuit`
        """
        qc = QubitCircuit(6)
        qc.add_gate("CNOT", targets=[1], controls=[0])
        test_gate = Gate("SWAP", targets=[1, 4])
        qc.add_gate(test_gate)
        qc.add_gate("TOFFOLI", controls=[0, 1], targets=[2])
        qc.add_gate("SNOT", targets=[3])
        qc.add_gate(test_gate, index=[3])
        qc.add_1q_gate("RY", start=4, end=5, arg_value=1.570796)

        # Test explicit gate addition
        assert qc.gates[0].name == "CNOT"
        assert qc.gates[0].targets == [1]
        assert qc.gates[0].controls == [0]

        # Test direct gate addition
        assert qc.gates[1].name == test_gate.name
        assert qc.gates[1].targets == test_gate.targets

        # Test specified position gate addition
        assert qc.gates[3].name == test_gate.name
        assert qc.gates[3].targets == test_gate.targets

        # Test adding 1 qubit gate on [start, end] qubits
        assert qc.gates[5].name == "RY"
        assert qc.gates[5].targets == [4]
        assert qc.gates[5].arg_value == 1.570796
        assert qc.gates[6].name == "RY"
        assert qc.gates[6].targets == [5]
        assert qc.gates[5].arg_value == 1.570796

        dummy_gate1 = Gate("DUMMY1")
        inds = [1, 3, 4, 6]
        qc.add_gate(dummy_gate1, index=inds)

        # Test adding gates at multiple (sorted) indices at once.
        # NOTE: Every insertion shifts the indices in the original list of
        #       gates by an additional position to the right.
        expected_gate_names = [
            'CNOT',     # 0
            'DUMMY1',   # 1
            'SWAP',     # 2
            'TOFFOLI',  # 3
            'DUMMY1',   # 4
            'SWAP',     # 5
            'DUMMY1',   # 6
            'SNOT',     # 7
            'RY',       # 8
            'DUMMY1',   # 9
            'RY',       # 10
        ]
        actual_gate_names = [gate.name for gate in qc.gates]
        assert actual_gate_names == expected_gate_names

        dummy_gate2 = Gate("DUMMY2")
        inds = [11, 0]
        qc.add_gate(dummy_gate2, index=inds)

        # Test adding gates at multiple (unsorted) indices at once.
        expected_gate_names = [
            'DUMMY2',   # 0
            'CNOT',     # 1
            'DUMMY1',   # 2
            'SWAP',     # 3
            'TOFFOLI',  # 4
            'DUMMY1',   # 5
            'SWAP',     # 6
            'DUMMY1',   # 7
            'SNOT',     # 8
            'RY',       # 9
            'DUMMY1',   # 10
            'RY',       # 11
            'DUMMY2',   # 12
        ]
        actual_gate_names = [gate.name for gate in qc.gates]
        assert actual_gate_names == expected_gate_names
Exemple #4
0
    def test_add_gate(self):
        """
        Addition of a gate object directly to a `QubitCircuit`
        """
        qc = QubitCircuit(6)
        qc.add_gate("CNOT", targets=[1], controls=[0])
        test_gate = Gate("SWAP", targets=[1, 4])
        qc.add_gate(test_gate)
        qc.add_gate("TOFFOLI", controls=[0, 1], targets=[2])
        qc.add_gate("SNOT", targets=[3])
        qc.add_gate(test_gate, index=[3])
        qc.add_1q_gate("RY", start=4, end=5, arg_value=1.570796)

        # Test explicit gate addition
        assert qc.gates[0].name == "CNOT"
        assert qc.gates[0].targets == [1]
        assert qc.gates[0].controls == [0]

        # Test direct gate addition
        assert qc.gates[1].name == test_gate.name
        assert qc.gates[1].targets == test_gate.targets

        # Test specified position gate addition
        assert qc.gates[3].name == test_gate.name
        assert qc.gates[3].targets == test_gate.targets

        # Test adding 1 qubit gate on [start, end] qubits
        assert qc.gates[5].name == "RY"
        assert qc.gates[5].targets == [4]
        assert qc.gates[5].arg_value == 1.570796
        assert qc.gates[6].name == "RY"
        assert qc.gates[6].targets == [5]
        assert qc.gates[5].arg_value == 1.570796

        # Test Exceptions  # Global phase is not included
        for gate in _single_qubit_gates:
            if gate not in _para_gates:
                # No target
                pytest.raises(ValueError, qc.add_gate, gate, None, None)
                # Multiple targets
                pytest.raises(ValueError, qc.add_gate, gate, [0, 1, 2], None)
                # With control
                pytest.raises(ValueError, qc.add_gate, gate, [0], [1])
            else:
                # No target
                pytest.raises(ValueError, qc.add_gate, gate, None, None, 1)
                # Multiple targets
                pytest.raises(ValueError, qc.add_gate, gate, [0, 1, 2], None,
                              1)
                # With control
                pytest.raises(ValueError, qc.add_gate, gate, [0], [1], 1)
        for gate in _ctrl_gates:
            if gate not in _para_gates:
                # No target
                pytest.raises(ValueError, qc.add_gate, gate, None, [1])
                # No control
                pytest.raises(ValueError, qc.add_gate, gate, [0], None)
            else:
                # No target
                pytest.raises(ValueError, qc.add_gate, gate, None, [1], 1)
                # No control
                pytest.raises(ValueError, qc.add_gate, gate, [0], None, 1)
        for gate in _swap_like:
            if gate not in _para_gates:
                # Single target
                pytest.raises(ValueError, qc.add_gate, gate, [0], None)
                # With control
                pytest.raises(ValueError, qc.add_gate, gate, [0, 1], [3])
            else:
                # Single target
                pytest.raises(ValueError, qc.add_gate, gate, [0], None, 1)
                # With control
                pytest.raises(ValueError, qc.add_gate, gate, [0, 1], [3], 1)
        for gate in _fredkin_like:
            # Single target
            pytest.raises(ValueError, qc.add_gate, gate, [0], [2])
            # No control
            pytest.raises(ValueError, qc.add_gate, gate, [0, 1], None)
        for gate in _toffoli_like:
            # No target
            pytest.raises(ValueError, qc.add_gate, gate, None, [1, 2])
            # Single control
            pytest.raises(ValueError, qc.add_gate, gate, [0], [1])