Exemplo n.º 1
0
    def test_zero_reps(self):
        """Test zero reps."""
        circuit = QAOAAnsatz(I ^ 4, reps=0)
        reference = QuantumCircuit(4)
        reference.h(range(4))

        self.assertEqual(circuit.decompose(), reference)
Exemplo n.º 2
0
    def test_default_qaoa(self):
        """Test construction of the default circuit."""
        circuit = QAOAAnsatz(I, 1)

        parameters = circuit.parameters

        circuit = circuit.decompose()
        self.assertEqual(1, len(parameters))
        self.assertIsInstance(circuit.data[0][0], HGate)
        self.assertIsInstance(circuit.decompose().data[1][0], RXGate)
Exemplo n.º 3
0
    def test_custom_operator_mixer(self):
        """Test circuit with a custom mixer as an operator."""
        mixer = Y
        circuit = QAOAAnsatz(cost_operator=I, reps=1, mixer_operator=mixer)

        parameters = circuit.parameters
        circuit = circuit.decompose()
        self.assertEqual(1, len(parameters))
        self.assertIsInstance(circuit.data[0][0], HGate)
        self.assertIsInstance(circuit.data[1][0], RYGate)
Exemplo n.º 4
0
    def test_custom_initial_state(self):
        """Test circuit with a custom initial state."""
        initial_state = QuantumCircuit(1)
        initial_state.y(0)
        circuit = QAOAAnsatz(initial_state=initial_state, cost_operator=I, reps=1)

        parameters = circuit.parameters
        circuit = circuit.decompose()
        self.assertEqual(1, len(parameters))
        self.assertIsInstance(circuit.data[0].operation, YGate)
        self.assertIsInstance(circuit.decompose().data[1].operation, RXGate)
Exemplo n.º 5
0
    def test_custom_circuit_mixer(self):
        """Test circuit with a custom mixer as a circuit"""
        mixer = QuantumCircuit(1)
        mixer.ry(1, 0)
        circuit = QAOAAnsatz(cost_operator=I, reps=1, mixer_operator=mixer)

        parameters = circuit.parameters
        circuit = circuit.decompose()
        self.assertEqual(0, len(parameters))
        self.assertIsInstance(circuit.data[0][0], HGate)
        self.assertIsInstance(circuit.data[1][0], RYGate)
Exemplo n.º 6
0
    def test_rebuild(self):
        """Test how a circuit can be rebuilt."""
        circuit = QAOAAnsatz(cost_operator=Z ^ I)  # circuit with 2 qubits
        # force circuit to be built
        _ = circuit.parameters

        circuit.cost_operator = Z  # now it only has 1 qubit
        circuit.reps = 5  # and now 5 repetitions
        # rebuild the circuit
        self.assertEqual(1, circuit.num_qubits)
        self.assertEqual(10, circuit.num_parameters)
Exemplo n.º 7
0
    def test_all_custom_parameters(self):
        """Test circuit with all custom parameters."""
        initial_state = QuantumCircuit(1)
        initial_state.y(0)
        mixer = Z

        circuit = QAOAAnsatz(
            cost_operator=I, reps=2, initial_state=initial_state, mixer_operator=mixer
        )

        parameters = circuit.parameters
        circuit = circuit.decompose()
        self.assertEqual(2, len(parameters))
        self.assertIsInstance(circuit.data[0].operation, YGate)
        self.assertIsInstance(circuit.data[1].operation, RZGate)
        self.assertIsInstance(circuit.data[2].operation, RZGate)
Exemplo n.º 8
0
 def _check_operator_ansatz(self, operator: OperatorBase) -> OperatorBase:
     # Recreates a circuit based on operator parameter.
     if operator != self._cost_operator:
         self._cost_operator = operator
         self.ansatz = QAOAAnsatz(
             operator, self._reps, initial_state=self._initial_state, mixer_operator=self._mixer
         ).decompose()  # TODO remove decompose once #6674 is fixed
Exemplo n.º 9
0
 def _check_operator_ansatz(self, operator: OperatorBase) -> OperatorBase:
     # Recreates a circuit based on operator parameter.
     if operator.num_qubits != self.ansatz.num_qubits:
         self.ansatz = QAOAAnsatz(operator,
                                  self._reps,
                                  initial_state=self._initial_state,
                                  mixer_operator=self._mixer)
Exemplo n.º 10
0
 def _check_operator(self, operator: OperatorBase) -> OperatorBase:
     # Recreates a circuit based on operator parameter.
     if operator.num_qubits != self.var_form.num_qubits:
         self.var_form = QAOAAnsatz(operator,
                                    self._reps,
                                    initial_state=self._initial_state,
                                    mixer_operator=self._mixer)
     operator = super()._check_operator(operator)
     return operator
Exemplo n.º 11
0
    def test_circuit_mixer(self):
        """Test using a parameterized circuit as mixer."""
        x1, x2 = Parameter("x1"), Parameter("x2")
        mixer = QuantumCircuit(2)
        mixer.rx(x1, 0)
        mixer.ry(x2, 1)

        reps = 4
        circuit = QAOAAnsatz(cost_operator=Z ^ Z, mixer_operator=mixer, reps=reps)
        self.assertEqual(circuit.num_parameters, 3 * reps)
Exemplo n.º 12
0
    def test_parameter_bounds(self):
        """Test the parameter bounds."""
        circuit = QAOAAnsatz(Z, reps=2)
        bounds = circuit.parameter_bounds

        for lower, upper in bounds[:2]:
            self.assertAlmostEqual(lower, 0)
            self.assertAlmostEqual(upper, 2 * np.pi)

        for lower, upper in bounds[2:]:
            self.assertIsNone(lower)
            self.assertIsNone(upper)
Exemplo n.º 13
0
 def test_identity(self):
     """Test construction with identity"""
     reps = 4
     num_qubits = 3
     pauli_sum_op = PauliSumOp.from_list([("I" * num_qubits, 1)])
     pauli_op = I ^ num_qubits
     for cost in [pauli_op, pauli_sum_op]:
         for mixer in [None, pauli_op, pauli_sum_op]:
             with self.subTest(f"cost: {type(cost)}, mixer:{type(mixer)}"):
                 circuit = QAOAAnsatz(cost_operator=cost, mixer_operator=mixer, reps=reps)
                 target = reps if mixer is None else 0
                 self.assertEqual(circuit.num_parameters, target)
Exemplo n.º 14
0
 def test_empty_op(self):
     """Test construction without cost operator"""
     circuit = QAOAAnsatz(reps=1)
     self.assertEqual(circuit.num_qubits, 0)
     with self.assertRaises(ValueError):
         circuit.decompose()
Exemplo n.º 15
0
 def test_invalid_reps(self):
     """Test negative reps."""
     circuit = QAOAAnsatz(I, reps=-1)
     with self.assertRaises(AttributeError):
         _ = circuit.count_ops()
Exemplo n.º 16
0
 def test_invalid_reps(self):
     """Test negative reps."""
     with self.assertRaises(ValueError):
         _ = QAOAAnsatz(I, reps=-1)
Exemplo n.º 17
0
    def test_configuration(self):
        """Test configuration checks."""
        mixer = QuantumCircuit(2)
        circuit = QAOAAnsatz(cost_operator=I, reps=1, mixer_operator=mixer)

        self.assertRaises(ValueError, lambda: circuit.parameters)
Exemplo n.º 18
0
    def test_num_qubits(self, num_qubits):
        """Test num_qubits with {num_qubits} qubits"""

        circuit = QAOAAnsatz(cost_operator=I ^ num_qubits, reps=5)
        self.assertEqual(circuit.num_qubits, num_qubits)