# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2019.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
QasmSimulator Integration Tests
"""


from test.terra.reference import ref_unitary_gate, ref_diagonal_gate

from qiskit import execute
from qiskit.providers.aer import QasmSimulator

import numpy as np

class QasmUnitaryGateTests:
    """QasmSimulator additional tests."""

    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    # ---------------------------------------------------------------------
    # Test unitary gate qobj instruction
    # ---------------------------------------------------------------------

    def test_unitary_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_unitary_gate.unitary_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_unitary_gate.unitary_gate_counts_deterministic(
            shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_random_unitary_gate(self):
        """Test simulation with random unitary gate circuit instructions."""
        shots = 2000
        circuits = ref_unitary_gate.unitary_random_gate_circuits_nondeterministic(final_measure=True)
        targets = ref_unitary_gate.unitary_random_gate_counts_nondeterministic()
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0.05 * shots)


class QasmDiagonalGateTests:
    """QasmSimulator additional tests."""

    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    # ---------------------------------------------------------------------
    # Test unitary gate qobj instruction
    # ---------------------------------------------------------------------

    def test_diagonal_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_diagonal_gate.diagonal_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_diagonal_gate.diagonal_gate_counts_deterministic(
            shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_diagonal_gate_wrapper(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        lsts = [
            [1, -1],
            [1, -1, -1, 1],
            [1.0, -1.0, -1.0, 1.0]]
        circuits = [ ref_diagonal_gate.diagonal_gate_circuits_deterministic_w(state)
            for state in [ np.array(lst, dtype=t) 
                for t in (None, float, np.float32, complex, np.complex64)
                    for lst in lsts  ] ]
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))

6d8964c999013cf9977488687b806acf9a02c107
<mask token>


class QasmUnitaryGateTests:
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def test_random_unitary_gate(self):
        """Test simulation with random unitary gate circuit instructions."""
        shots = 2000
        circuits = (ref_unitary_gate.
            unitary_random_gate_circuits_nondeterministic(final_measure=True))
        targets = ref_unitary_gate.unitary_random_gate_counts_nondeterministic(
            )
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0.05 * shots)


class QasmDiagonalGateTests:
    """QasmSimulator additional tests."""
    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    def test_diagonal_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_diagonal_gate.diagonal_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_diagonal_gate.diagonal_gate_counts_deterministic(shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_diagonal_gate_wrapper(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        lsts = [[1, -1], [1, -1, -1, 1], [1.0, -1.0, -1.0, 1.0]]
        circuits = [ref_diagonal_gate.
            diagonal_gate_circuits_deterministic_w(state) for state in [np.
            array(lst, dtype=t) for t in (None, float, np.float32, complex,
            np.complex64) for lst in lsts]]
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))

<mask token>


class QasmUnitaryGateTests:
    <mask token>
    <mask token>
    <mask token>

    def test_unitary_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_unitary_gate.unitary_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_unitary_gate.unitary_gate_counts_deterministic(shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_random_unitary_gate(self):
        """Test simulation with random unitary gate circuit instructions."""
        shots = 2000
        circuits = (ref_unitary_gate.
            unitary_random_gate_circuits_nondeterministic(final_measure=True))
        targets = ref_unitary_gate.unitary_random_gate_counts_nondeterministic(
            )
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0.05 * shots)


class QasmDiagonalGateTests:
    """QasmSimulator additional tests."""
    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    def test_diagonal_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_diagonal_gate.diagonal_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_diagonal_gate.diagonal_gate_counts_deterministic(shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_diagonal_gate_wrapper(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        lsts = [[1, -1], [1, -1, -1, 1], [1.0, -1.0, -1.0, 1.0]]
        circuits = [ref_diagonal_gate.
            diagonal_gate_circuits_deterministic_w(state) for state in [np.
            array(lst, dtype=t) for t in (None, float, np.float32, complex,
            np.complex64) for lst in lsts]]
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))

<mask token>


class QasmUnitaryGateTests:
    <mask token>
    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    def test_unitary_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_unitary_gate.unitary_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_unitary_gate.unitary_gate_counts_deterministic(shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_random_unitary_gate(self):
        """Test simulation with random unitary gate circuit instructions."""
        shots = 2000
        circuits = (ref_unitary_gate.
            unitary_random_gate_circuits_nondeterministic(final_measure=True))
        targets = ref_unitary_gate.unitary_random_gate_counts_nondeterministic(
            )
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0.05 * shots)


class QasmDiagonalGateTests:
    """QasmSimulator additional tests."""
    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    def test_diagonal_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_diagonal_gate.diagonal_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_diagonal_gate.diagonal_gate_counts_deterministic(shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_diagonal_gate_wrapper(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        lsts = [[1, -1], [1, -1, -1, 1], [1.0, -1.0, -1.0, 1.0]]
        circuits = [ref_diagonal_gate.
            diagonal_gate_circuits_deterministic_w(state) for state in [np.
            array(lst, dtype=t) for t in (None, float, np.float32, complex,
            np.complex64) for lst in lsts]]
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))

<mask token>


class QasmUnitaryGateTests:
    """QasmSimulator additional tests."""
    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    def test_unitary_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_unitary_gate.unitary_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_unitary_gate.unitary_gate_counts_deterministic(shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_random_unitary_gate(self):
        """Test simulation with random unitary gate circuit instructions."""
        shots = 2000
        circuits = (ref_unitary_gate.
            unitary_random_gate_circuits_nondeterministic(final_measure=True))
        targets = ref_unitary_gate.unitary_random_gate_counts_nondeterministic(
            )
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0.05 * shots)


class QasmDiagonalGateTests:
    """QasmSimulator additional tests."""
    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    def test_diagonal_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_diagonal_gate.diagonal_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_diagonal_gate.diagonal_gate_counts_deterministic(shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_diagonal_gate_wrapper(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        lsts = [[1, -1], [1, -1, -1, 1], [1.0, -1.0, -1.0, 1.0]]
        circuits = [ref_diagonal_gate.
            diagonal_gate_circuits_deterministic_w(state) for state in [np.
            array(lst, dtype=t) for t in (None, float, np.float32, complex,
            np.complex64) for lst in lsts]]
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))

# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2019.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
QasmSimulator Integration Tests
"""


from test.terra.reference import ref_unitary_gate, ref_diagonal_gate

from qiskit import execute
from qiskit.providers.aer import QasmSimulator

import numpy as np

class QasmUnitaryGateTests:
    """QasmSimulator additional tests."""

    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    # ---------------------------------------------------------------------
    # Test unitary gate qobj instruction
    # ---------------------------------------------------------------------

    def test_unitary_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_unitary_gate.unitary_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_unitary_gate.unitary_gate_counts_deterministic(
            shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_random_unitary_gate(self):
        """Test simulation with random unitary gate circuit instructions."""
        shots = 2000
        circuits = ref_unitary_gate.unitary_random_gate_circuits_nondeterministic(final_measure=True)
        targets = ref_unitary_gate.unitary_random_gate_counts_nondeterministic()
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0.05 * shots)


class QasmDiagonalGateTests:
    """QasmSimulator additional tests."""

    SIMULATOR = QasmSimulator()
    BACKEND_OPTS = {}

    # ---------------------------------------------------------------------
    # Test unitary gate qobj instruction
    # ---------------------------------------------------------------------

    def test_diagonal_gate(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        circuits = ref_diagonal_gate.diagonal_gate_circuits_deterministic(
            final_measure=True)
        targets = ref_diagonal_gate.diagonal_gate_counts_deterministic(
            shots)
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
        self.compare_counts(result, circuits, targets, delta=0)

    def test_diagonal_gate_wrapper(self):
        """Test simulation with unitary gate circuit instructions."""
        shots = 100
        lsts = [
            [1, -1],
            [1, -1, -1, 1],
            [1.0, -1.0, -1.0, 1.0]]
        circuits = [ ref_diagonal_gate.diagonal_gate_circuits_deterministic_w(state)
            for state in [ np.array(lst, dtype=t) 
                for t in (None, float, np.float32, complex, np.complex64)
                    for lst in lsts  ] ]
        result = execute(circuits, self.SIMULATOR, shots=shots).result()
        self.assertTrue(getattr(result, 'success', False))
