Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
fix write to dir, use new tempfile functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryoris committed Apr 27, 2020
1 parent e30789c commit 4253c89
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
7 changes: 4 additions & 3 deletions qiskit/optimization/problems/quadratic_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,11 @@ def maximize(self,

def from_docplex(self, model: Model) -> None:
"""Loads this quadratic program from a docplex model.
Note that this supports only basic functions of docplex as follows:
- quadratic objective function
- linear / quadratic constraints
- binary / integer / continuous variables
- quadratic objective function
- linear / quadratic constraints
- binary / integer / continuous variables
Args:
model: The docplex model to be loaded.
Expand Down
40 changes: 15 additions & 25 deletions test/optimization/test_quadratic_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import unittest
import tempfile
import os
from test.optimization.optimization_test_case import QiskitOptimizationTestCase

from docplex.mp.model import Model, DOcplexException
Expand All @@ -29,19 +28,6 @@ class TestQuadraticProgram(QiskitOptimizationTestCase):
"""Test QuadraticProgram without the members that have separate test classes
(VariablesInterface, etc)."""

def setUp(self):
file, self.temp_output_file = tempfile.mkstemp(suffix='.lp')
os.close(file)
file, self.temp_problem_file = tempfile.mkstemp(suffix='.lp')
os.close(file)

def tearDown(self):
for temp in [self.temp_output_file, self.temp_problem_file]:
try:
os.remove(temp)
except OSError:
pass

def test_constructor(self):
""" test constructor """
quadratic_program = QuadraticProgram()
Expand Down Expand Up @@ -494,20 +480,24 @@ def test_write_to_lp_file(self):
'<=', 1, 'quad_leq')
q_p.quadratic_constraint({'x': 1, 'y': 1}, {('x', 'x'): 1, ('y', 'z'): -1, ('z', 'z'): 2},
'>=', 1, 'quad_geq')
q_p.write_to_lp_file(self.temp_output_file)
with open(self.temp_output_file) as file1, open(
'test/optimization/resources/test_quadratic_program.lp') as file2:
lines1 = file1.readlines()
lines2 = file2.readlines()
self.assertListEqual(lines1, lines2)

q_p.write_to_lp_file(self.temp_problem_file)
with open(self.temp_problem_file) as file1, open(
'test/optimization/resources/test_quadratic_program.lp') as file2:
lines1 = file1.readlines()
lines2 = file2.readlines()
temp_output_file = tempfile.NamedTemporaryFile(mode='w+t', suffix='.lp')
q_p.write_to_lp_file(temp_output_file.name)
with open('test/optimization/resources/test_quadratic_program.lp') as reference:
lines1 = temp_output_file.readlines()
lines2 = reference.readlines()
self.assertListEqual(lines1, lines2)

temp_output_file.close() # automatically deleted

with tempfile.TemporaryDirectory() as temp_problem_dir:
q_p.write_to_lp_file(temp_problem_dir)
with open(temp_problem_dir + '/my_problem.lp') as file1, open(
'test/optimization/resources/test_quadratic_program.lp') as file2:
lines1 = file1.readlines()
lines2 = file2.readlines()
self.assertListEqual(lines1, lines2)

with self.assertRaises(OSError):
q_p.write_to_lp_file('/cannot/write/this/file.lp')

Expand Down

0 comments on commit 4253c89

Please sign in to comment.