Exemplo n.º 1
0
def test_serialize_t_gate():
    q0 = cirq.LineQubit(0)
    serializer = ionq.Serializer()
    circuit = cirq.Circuit(cirq.Z(q0)**(0.25))
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(body={
        'qubits':
        1,
        'circuit': [{
            'gate': 't',
            'targets': [0]
        }]
    },
                                            metadata={})
    circuit = cirq.Circuit(cirq.Z(q0)**(-0.25))
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(body={
        'qubits':
        1,
        'circuit': [{
            'gate': 'ti',
            'targets': [0]
        }]
    },
                                            metadata={})
Exemplo n.º 2
0
def test_serialize_sqrt_x_gate():
    q0 = cirq.LineQubit(0)
    serializer = ionq.Serializer()
    circuit = cirq.Circuit(cirq.X(q0) ** (0.5))
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={'gateset': 'qis', 'qubits': 1, 'circuit': [{'gate': 'v', 'targets': [0]}]},
        metadata={},
    )
    circuit = cirq.Circuit(cirq.X(q0) ** (-0.5))
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={'gateset': 'qis', 'qubits': 1, 'circuit': [{'gate': 'vi', 'targets': [0]}]},
        metadata={},
    )
Exemplo n.º 3
0
def test_ionq_client_create_job(mock_post):
    mock_post.return_value.status_code.return_value = requests.codes.ok
    mock_post.return_value.json.return_value = {'foo': 'bar'}

    client = ionq.ionq_client._IonQClient(remote_host='http://example.com',
                                          api_key='to_my_heart')
    program = ionq.SerializedProgram(body={'job': 'mine'},
                                     metadata={'a': '0,1'})
    response = client.create_job(serialized_program=program,
                                 repetitions=200,
                                 target='qpu',
                                 name='bacon')
    assert response == {'foo': 'bar'}

    expected_json = {
        'target': 'qpu',
        'lang': 'json',
        'body': {
            'job': 'mine'
        },
        'name': 'bacon',
        'shots': '200',
        'metadata': {
            'shots': '200',
            'a': '0,1'
        },
    }
    expected_headers = {
        'Authorization': 'apiKey to_my_heart',
        'Content-Type': 'application/json'
    }
    mock_post.assert_called_with('http://example.com/v0.1/jobs',
                                 json=expected_json,
                                 headers=expected_headers)
Exemplo n.º 4
0
def test_serialize_measurement_gate():
    q0 = cirq.LineQubit(0)
    circuit = cirq.Circuit(cirq.measure(q0, key='tomyheart'))
    serializer = ionq.Serializer()
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={'qubits': 1, 'circuit': []}, metadata={'measurement0': f'tomyheart{chr(31)}0'}
    )
Exemplo n.º 5
0
def test_ionq_client_create_job_default_target(mock_post):
    mock_post.return_value.status_code.return_value = requests.codes.ok
    mock_post.return_value.json.return_value = {'foo'}

    client = ionq.ionq_client._IonQClient(
        remote_host='http://example.com', api_key='to_my_heart', default_target='simulator'
    )
    _ = client.create_job(ionq.SerializedProgram(body={'job': 'mine'}, metadata={}))
    assert mock_post.call_args[1]['json']['target'] == 'simulator'
Exemplo n.º 6
0
def test_serialize_pauli_gates():
    q0 = cirq.LineQubit(0)
    serializer = ionq.Serializer()
    for gate, name in ((cirq.X, 'x'), (cirq.Y, 'y'), (cirq.Z, 'z')):
        circuit = cirq.Circuit(gate(q0))
        result = serializer.serialize(circuit)
        assert result == ionq.SerializedProgram(
            body={'qubits': 1, 'circuit': [{'gate': name, 'targets': [0]}]}, metadata={}
        )
Exemplo n.º 7
0
def test_serialize_measurement_gate_multiple_keys():
    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit(cirq.measure(q0, key='a'), cirq.measure(q1, key='b'))
    serializer = ionq.Serializer()
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={'qubits': 2, 'circuit': []},
        metadata={'measurement0': f'a{chr(31)}0{chr(30)}b{chr(31)}1'},
    )
Exemplo n.º 8
0
def test_serialize_measurement_gate_target_order():
    q0, _, q2 = cirq.LineQubit.range(3)
    circuit = cirq.Circuit(cirq.measure(q2, q0, key='tomyheart'))
    serializer = ionq.Serializer()
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={'qubits': 3, 'circuit': []},
        metadata={'measurement0': f'tomyheart{chr(31)}2,0'},
    )
Exemplo n.º 9
0
def test_ionq_client_create_job_retry_request_error(mock_post):
    response2 = mock.MagicMock()
    mock_post.side_effect = [requests.exceptions.ConnectionError(), response2]
    response2.ok = True
    client = ionq.ionq_client._IonQClient(remote_host='http://example.com',
                                          api_key='to_my_heart',
                                          default_target='simulator')
    _ = client.create_job(serialized_program=ionq.SerializedProgram(
        body={'job': 'mine'}, metadata={}))
    assert mock_post.call_count == 2
Exemplo n.º 10
0
def test_ionq_client_create_job_not_retriable(mock_post):
    mock_post.return_value.ok = False
    mock_post.return_value.status_code = requests.codes.not_implemented

    client = ionq.ionq_client._IonQClient(remote_host='http://example.com',
                                          api_key='to_my_heart',
                                          default_target='simulator')
    with pytest.raises(ionq.IonQException, match='Status: 501'):
        _ = client.create_job(serialized_program=ionq.SerializedProgram(
            body={'job': 'mine'}, metadata={}))
Exemplo n.º 11
0
def test_serialize_h_gate():
    q0 = cirq.LineQubit(0)
    serializer = ionq.Serializer()
    circuit = cirq.Circuit(cirq.H(q0))
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={'qubits': 1, 'circuit': [{'gate': 'h', 'targets': [0]}]}, metadata={}
    )

    with pytest.raises(ValueError, match=r'H\*\*0.5'):
        circuit = cirq.Circuit(cirq.H(q0) ** 0.5)
        _ = serializer.serialize(circuit)
Exemplo n.º 12
0
def test_serialize_swap_gate():
    q0, q1 = cirq.LineQubit.range(2)
    serializer = ionq.Serializer()
    circuit = cirq.Circuit(cirq.SWAP(q0, q1))
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={'qubits': 2, 'circuit': [{'gate': 'swap', 'targets': [0, 1]}]}, metadata={}
    )

    with pytest.raises(ValueError, match=r'SWAP\*\*0.5'):
        circuit = cirq.Circuit(cirq.SWAP(q0, q1) ** 0.5)
        _ = serializer.serialize(circuit)
Exemplo n.º 13
0
def test_serialize_cnot_gate():
    q0, q1 = cirq.LineQubit.range(2)
    serializer = ionq.Serializer()
    circuit = cirq.Circuit(cirq.CNOT(q0, q1))
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={'qubits': 2, 'circuit': [{'gate': 'cnot', 'control': 0, 'target': 1}]}, metadata={}
    )

    with pytest.raises(ValueError, match=r'CNOT\*\*0.5'):
        circuit = cirq.Circuit(cirq.CNOT(q0, q1) ** 0.5)
        _ = serializer.serialize(circuit)
Exemplo n.º 14
0
def test_ionq_client_create_job_timeout(mock_post):
    mock_post.return_value.ok = False
    mock_post.return_value.status_code = requests.codes.service_unavailable

    client = ionq.ionq_client._IonQClient(
        remote_host='http://example.com',
        api_key='to_my_heart',
        default_target='simulator',
        max_retry_seconds=0.2,
    )
    with pytest.raises(TimeoutError):
        _ = client.create_job(serialized_program=ionq.SerializedProgram(
            body={'job': 'mine'}, metadata={}))
Exemplo n.º 15
0
def test_serialize_pow_gates():
    q0 = cirq.LineQubit(0)
    serializer = ionq.Serializer()
    for name, gate in (('rx', cirq.X), ('ry', cirq.Y), ('rz', cirq.Z)):
        for exponent in (1.1, 0.6):
            circuit = cirq.Circuit((gate ** exponent)(q0))
            result = serializer.serialize(circuit)
            assert result == ionq.SerializedProgram(
                body={
                    'qubits': 1,
                    'circuit': [{'gate': name, 'targets': [0], 'rotation': exponent * np.pi}],
                },
                metadata={},
            )
Exemplo n.º 16
0
def test_serialize_parity_pow_gate():
    q0, q1 = cirq.LineQubit.range(2)
    serializer = ionq.Serializer()
    for gate, name in ((cirq.XXPowGate, 'xx'), (cirq.YYPowGate, 'yy'), (cirq.ZZPowGate, 'zz')):
        for exponent in (0.5, 1.0, 1.5):
            circuit = cirq.Circuit(gate(exponent=exponent)(q0, q1))
            result = serializer.serialize(circuit)
            assert result == ionq.SerializedProgram(
                body={
                    'qubits': 2,
                    'circuit': [{'gate': name, 'targets': [0, 1], 'rotation': exponent * np.pi}],
                },
                metadata={},
            )
Exemplo n.º 17
0
def test_ionq_client_create_job_retry(mock_post):
    response1 = mock.MagicMock()
    response2 = mock.MagicMock()
    mock_post.side_effect = [response1, response2]
    response1.ok = False
    response1.status_code = requests.codes.service_unavailable
    response2.ok = True
    client = ionq.ionq_client._IonQClient(
        remote_host='http://example.com',
        api_key='to_my_heart',
        default_target='simulator',
        verbose=True,
    )
    test_stdout = io.StringIO()
    with contextlib.redirect_stdout(test_stdout):
        _ = client.create_job(serialized_program=ionq.SerializedProgram(
            body={'job': 'mine'}, metadata={}))
    assert test_stdout.getvalue().strip(
    ) == 'Waiting 0.1 seconds before retrying.'
    assert mock_post.call_count == 2
Exemplo n.º 18
0
def test_serialize_native_gates():
    q0, q1, q2 = cirq.LineQubit.range(3)
    gpi = GPIGate(phi=0.1).on(q0)
    gpi2 = GPI2Gate(phi=0.2).on(q1)
    ms = MSGate(phi0=0.3, phi1=0.4).on(q1, q2)
    circuit = cirq.Circuit([gpi, gpi2, ms])
    serializer = ionq.Serializer()
    result = serializer.serialize(circuit)
    assert result == ionq.SerializedProgram(
        body={
            'gateset': 'native',
            'qubits': 3,
            'circuit': [
                {'gate': 'gpi', 'target': 0, 'phase': 0.1},
                {'gate': 'gpi2', 'target': 1, 'phase': 0.2},
                {'gate': 'ms', 'targets': [1, 2], 'phases': [0.3, 0.4]},
            ],
        },
        metadata={},
    )
Exemplo n.º 19
0
def test_ionq_client_create_job_no_targets():
    client = ionq.ionq_client._IonQClient(remote_host='http://example.com',
                                          api_key='to_my_heart')
    with pytest.raises(AssertionError, match='neither were set'):
        _ = client.create_job(serialized_program=ionq.SerializedProgram(
            body={'job': 'mine'}, metadata={}))