From 7f38f0010b960fbf3a93bb7ec48c392373221d34 Mon Sep 17 00:00:00 2001 From: Peter Karalekas Date: Thu, 3 Jan 2019 14:00:56 -0800 Subject: [PATCH] Add qvm/quilc to the unit tests with gitlab CI services (#752) * Add rigetticomputing/qvm as a gitlab CI service * Change the qvm fixture endpoint to be http://qvm:5000 * Change mock_qvm to qvm in test_sync_expectation_2 * Change the endpoint of the mock_qvm in test_api.py to be qvm instead of localhost * Missed a couple of the localhost->qvm changes in test_api.py * Export the QVM_URL in the gitlab CI yaml * Remove test_seeded_qvm because it is very confusing * Remove the hardcoding of the qvm endpoint and instead use the QVM_URL env variable * Remove the test_config_parsing UT as it breaks when env vars are set * Add quilc as a service and set the COMPILER_URL var in the CI yaml --- .gitlab-ci.yml | 11 +++++++ pyquil/tests/test_api.py | 67 +++++----------------------------------- 2 files changed, 19 insertions(+), 59 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fe0c09a65..0b575e58f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,11 +1,22 @@ before_script: - pip install tox +services: + - name: rigetticomputing/qvm + alias: qvm + command: ["-S"] + - name: rigetticomputing/quilc + alias: quilc + command: ["-S"] + + test: tags: - docker - python:3.6 script: + - export QVM_URL="http://qvm:5000" + - export COMPILER_URL='http://quilc:6000' - tox -e py36 style: diff --git a/pyquil/tests/test_api.py b/pyquil/tests/test_api.py index a98dfba13..60cd6843c 100644 --- a/pyquil/tests/test_api.py +++ b/pyquil/tests/test_api.py @@ -70,6 +70,7 @@ RB_REPLY = [Program("H 0\nH 0\n"), Program("PHASE(pi/2) 0\nPHASE(pi/2) 0\n")] mock_qvm = QVMConnection() +mock_endpoint = mock_qvm.sync_endpoint def test_sync_run_mock(): @@ -83,13 +84,13 @@ def mock_response(request, context): return '{"ro": [[0,0],[1,1]]}' with requests_mock.Mocker() as m: - m.post('http://127.0.0.1:5000/qvm', text=mock_response) + m.post(mock_endpoint + '/qvm', text=mock_response) assert mock_qvm.run(BELL_STATE_MEASURE, [0, 1], trials=2) == [[0, 0], [1, 1]] # Test no classical addresses - m.post('http://127.0.0.1:5000/qvm', text=mock_response) + m.post(mock_endpoint + '/qvm', text=mock_response) assert mock_qvm.run(BELL_STATE_MEASURE, trials=2) == [[0, 0], [1, 1]] with pytest.raises(ValueError): @@ -123,7 +124,7 @@ def mock_response(request, context): return '[[0,0],[1,1]]' with requests_mock.Mocker() as m: - m.post('http://127.0.0.1:5000/qvm', text=mock_response) + m.post(mock_endpoint + '/qvm', text=mock_response) assert mock_qvm.run_and_measure(BELL_STATE, [0, 1], trials=2) == [[0, 0], [1, 1]] with pytest.raises(ValueError): @@ -155,13 +156,13 @@ def mock_response(request, context): return b'[0.0, 0.0, 1.0]' with requests_mock.Mocker() as m: - m.post('http://127.0.0.1:5000/qvm', content=mock_response) + m.post(mock_endpoint + '/qvm', content=mock_response) result = mock_qvm.expectation(BELL_STATE, [Program(Z(0)), Program(Z(1)), Program(Z(0), Z(1))]) exp_expected = [0.0, 0.0, 1.0] np.testing.assert_allclose(exp_expected, result) with requests_mock.Mocker() as m: - m.post('http://127.0.0.1:5000/qvm', content=mock_response) + m.post(mock_endpoint + '/qvm', content=mock_response) z0 = PauliTerm("Z", 0) z1 = PauliTerm("Z", 1) z01 = z0 * z1 @@ -180,7 +181,7 @@ def test_sync_expectation_2(qvm): z0 = PauliTerm("Z", 0) z1 = PauliTerm("Z", 1) z01 = z0 * z1 - result = mock_qvm.pauli_expectation(BELL_STATE, [z0, z1, z01]) + result = qvm.pauli_expectation(BELL_STATE, [z0, z1, z01]) exp_expected = [0.0, 0.0, 1.0] np.testing.assert_allclose(exp_expected, result) @@ -195,7 +196,7 @@ def mock_response(request, context): return b'[1.0, 0.0, 0.0]' with requests_mock.Mocker() as m: - m.post('http://127.0.0.1:5000/qvm', content=mock_response) + m.post(mock_endpoint + '/qvm', content=mock_response) z0 = PauliTerm("Z", 0) z1 = PauliTerm("Z", 1) z01 = z0 * z1 @@ -211,43 +212,6 @@ def test_sync_wavefunction(qvm): np.testing.assert_allclose(result.amplitudes, wf_expected) -def test_seeded_qvm(test_device): - def mock_response(request, context): - assert json.loads(request.text) == { - "type": "multishot-measure", - "qubits": [0, 1], - "trials": 2, - "compiled-quil": "H 0\nCNOT 0 1\n" - } - return '[[0,0],[1,1]]' - - with patch.object(LocalQVMCompiler, "quil_to_native_quil") as m_compile,\ - patch('pyquil.api._qvm.apply_noise_model') as m_anm,\ - requests_mock.Mocker() as m: - m.post('http://127.0.0.1:5000/qvm', text=mock_response) - m_compile.side_effect = [BELL_STATE] - m_anm.side_effect = [BELL_STATE] - - qvm = QVMConnection(test_device) - assert qvm.noise_model == test_device.noise_model - qvm.run_and_measure(BELL_STATE, qubits=[0, 1], trials=2) - assert m_compile.call_count == 1 - assert m_anm.call_count == 1 - - test_device.noise_model = None - qvm = QVMConnection(test_device) - assert qvm.noise_model is None - qvm.run_and_measure(BELL_STATE, qubits=[0, 1], trials=2) - assert m_compile.call_count == 1 - assert m_anm.call_count == 1 - - qvm = QVMConnection() - assert qvm.noise_model is None - qvm.run_and_measure(BELL_STATE, qubits=[0, 1], trials=2) - assert m_compile.call_count == 1 - assert m_anm.call_count == 1 - - def test_validate_noise_probabilities(): with pytest.raises(TypeError): validate_noise_probabilities(1) @@ -273,21 +237,6 @@ def test_prepare_register_list(): prepare_register_list({'ro': [-1, 1]}) -def test_config_parsing(): - with patch.dict('os.environ', {"FOREST_CONFIG": os.path.join(os.path.dirname(__file__), - "data/forest_config.test"), - "QCS_CONFIG": os.path.join(os.path.dirname(__file__), - "data/qcs_config.test")}): - pq_config = PyquilConfig() - assert pq_config.forest_url == "http://dummy_forest_url" - assert pq_config.api_key == "pyquil_user_key" - assert pq_config.user_id == "pyquil_user_token" - assert pq_config.engage_cmd == "dummy_command" - assert pq_config.qpu_url == "tcp://dummy_qpu_url" - assert pq_config.qvm_url == "http://dummy_qvm_url" - assert pq_config.compiler_url == "tcp://dummy_compiler_server_url" - - # --------------------- # compiler-server tests # ---------------------