Skip to content

Commit

Permalink
Add qvm/quilc to the unit tests with gitlab CI services (#752)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
karalekas authored Jan 3, 2019
1 parent 6b0d03d commit 7f38f00
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 59 deletions.
11 changes: 11 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
67 changes: 8 additions & 59 deletions pyquil/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
# ---------------------
Expand Down

0 comments on commit 7f38f00

Please sign in to comment.