def test_insert_barriers(self):
        """Test using insert_barriers."""
        evo = EvolvedOperatorAnsatz(Z, reps=4, insert_barriers=True)
        evo._build()
        ref = QuantumCircuit(1)
        for parameter in evo.parameters:
            ref.rz(2.0 * parameter, 0)
            # ref.rx(2.0 * parameter, 0)
            if parameter != evo.parameters[-1]:
                ref.barrier()

        self.assertEqual(evo, ref)
    def test_custom_evolution(self):
        """Test using another evolution than the default (e.g. matrix evolution)."""

        op = X ^ I ^ Z
        matrix = op.to_matrix()
        evo = EvolvedOperatorAnsatz(op, evolution=MatrixEvolution())
        evo._build()

        parameters = evo.parameters
        reference = QuantumCircuit(3)
        reference.hamiltonian(matrix, parameters[0], [0, 1, 2])

        self.assertEqual(evo, reference)
    def test_changing_operators(self):
        """Test rebuilding after the operators changed."""

        ops = [X, Y, Z]
        evo = EvolvedOperatorAnsatz(ops)
        evo.operators = [X, Y]
        evo._build()

        parameters = evo.parameters
        reference = QuantumCircuit(1)
        reference.rx(2 * parameters[0], 0)
        reference.ry(2 * parameters[1], 0)

        self.assertEqual(evo, reference)
    def test_evolved_op_ansatz(self):
        """Test the default evolution."""
        num_qubits = 3

        ops = [Z ^ num_qubits, Y ^ num_qubits, X ^ num_qubits]
        strings = ["z" * num_qubits, "y" * num_qubits, "x" * num_qubits] * 2

        evo = EvolvedOperatorAnsatz(ops, 2)
        evo._build()  # fixed by speedup parameter binds PR

        reference = QuantumCircuit(num_qubits)
        parameters = evo.parameters
        for string, time in zip(strings, parameters):
            reference.compose(evolve(string, time), inplace=True)

        self.assertEqual(evo, reference)
 def test_invalid_reps(self):
     """Test setting an invalid number of reps."""
     evo = EvolvedOperatorAnsatz(X, reps=0)
     with self.assertRaises(ValueError):
         _ = evo.count_ops()