class_docstr: Integration tests for the pennylane.qnn.keras.KerasLayer class for
  density_matrix() returning circuits.
class_docstr_tok: ["Integration","tests","for","the","pennylane.qnn.keras.KerasLayer","class","for","density_matrix","(",")","returning","circuits","."]
docstr: Test if the model_dm can be successfully saved and reloaded using the get_weights()
  method
docstr_tok: ["Test","if","the","model_dm","can","be","successfully","saved","and","reloaded","using","the","get_weights","(",")","method"]
code: def test_save_model_weights_dm(
      self, get_circuit_dm, n_qubits, output_dim, tmpdir
  ):  # pylint: disable=no-self-use,redefined-outer-name
      """Test if the model_dm can be successfully saved and reloaded using the get_weights()
      method"""
      clayer = tf.keras.layers.Dense(
          n_qubits, use_bias=False, input_shape=(n_qubits,)
      )
      qlayer = KerasLayer(*get_circuit_dm, output_dim)
      model = tf.keras.models.Sequential([clayer, qlayer])
      weights = model.get_weights()
  
      file = str(tmpdir) + "/model"
      model.save_weights(file)
  
      new_clayer = tf.keras.layers.Dense(
          n_qubits, use_bias=False, input_shape=(n_qubits,)
      )
      new_qlayer = KerasLayer(*get_circuit_dm, output_dim)
      new_model = tf.keras.models.Sequential([new_clayer, new_qlayer])
      new_weights = new_model.get_weights()
  
      assert len(weights) == len(new_weights)
      assert all(
          w.shape == nw.shape for w, nw in zip(weights, new_weights)
      )
  
      # assert that the new model's weights are different
      assert all(
          tf.math.reduce_any(w != nw)
          for w, nw in zip(weights, new_weights)
      )
  
      new_model.load_weights(file)
      new_weights = new_model.get_weights()
  
      assert len(weights) == len(new_weights)
      assert all(
          w.shape == nw.shape for w, nw in zip(weights, new_weights)
      )
  
      # assert that the new model's weights are now the same
      assert all(
          tf.math.reduce_all(w == nw)
          for w, nw in zip(weights, new_weights)
      )
  
      # assert that the results are the same
      x = tf.constant(np.arange(5 * n_qubits).reshape(5, n_qubits))
      res = model(x)
      new_res = new_model(x)
      assert tf.math.reduce_all(res == new_res)
  
imports: from collections import defaultdict
  import numpy
  import pytest
  import pennylane
label_desc: Use image node_id 3 for calling the TestKerasLayerIntegrationDM obj's underlying member method code with example usage: obj.test_save_model_weights_dm(get_circuit_dm, n_qubits, output_dim, tmpdir) without return types