class_docstr: Integration tests for the pennylane.qnn.torch.TorchLayer class.
class_docstr_tok: ["Integration","tests","for","the","pennylane.qnn.torch.TorchLayer","class","."]
docstr: Test if the model can be saved and loaded
docstr_tok: ["Test","if","the","model","can","be","saved","and","loaded"]
code: def test_save_model(self, get_circuit, n_qubits, output_dim, tmpdir):
      """Test if the model can be saved and loaded"""
      qlayer = TorchLayer(*get_circuit)
      clayer = torch.nn.Linear(output_dim, 2)
      model = torch.nn.Sequential(qlayer, clayer)
  
      saved_state_dict = model.state_dict()
      path = str(tmpdir) + "/dummy.pt"
      torch.save(saved_state_dict, path)
  
      new_qlayer = TorchLayer(*get_circuit)
      new_clayer = torch.nn.Linear(output_dim, 2)
      new_model = torch.nn.Sequential(new_qlayer, new_clayer)
      new_state_dict = new_model.state_dict()
  
      assert list(saved_state_dict.keys()) == list(
          new_state_dict.keys()
      )
  
      # test that the new model's weights are different
      assert all(
          torch.any(saved_state_dict[k] != new_state_dict[k])
          for k in saved_state_dict.keys()
      )
  
      new_model.load_state_dict(torch.load(path))
      new_state_dict = new_model.state_dict()
  
      assert list(saved_state_dict.keys()) == list(
          new_state_dict.keys()
      )
  
      # test that the new model's weights are now the same
      assert all(
          torch.all(saved_state_dict[k] == new_state_dict[k])
          for k in saved_state_dict.keys()
      )
  
imports: from unittest import mock
  import math
  from collections import defaultdict
  import numpy
  import pytest
  import pennylane
  from pennylane.qnn.torch import TorchLayer
label_desc: Use image node_id 4 for calling the TestTorchLayerIntegration obj's underlying member method code with example usage: obj.test_save_model(get_circuit, n_qubits, output_dim, tmpdir) without return types