def _bell_ir_with_result(targets=None):
     return Program.parse_raw(
         json.dumps({
             "instructions": [
                 {
                     "type": "h",
                     "target": 0
                 },
                 {
                     "type": "cnot",
                     "target": 1,
                     "control": 0
                 },
             ],
             "results": [
                 {
                     "type": "amplitude",
                     "states": ["11"]
                 },
                 {
                     "type": "expectation",
                     "observable": ["x"],
                     "targets": targets
                 },
             ],
         }))
def test_simulator_run_result_types_shots_basis_rotation_gates_value_error():
    simulator = DefaultSimulator()
    ir = Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ],
            "basis_rotation_instructions": [{
                "type": "foo",
                "target": 1
            }],
            "results": [{
                "type": "expectation",
                "observable": ["x"],
                "targets": [1]
            }],
        }))
    shots_count = 1000
    simulator.run(ir, qubit_count=2, shots=shots_count)
def test_simulator_run_result_types_shots_basis_rotation_gates():
    simulator = DefaultSimulator()
    ir = Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ],
            "basis_rotation_instructions": [{
                "type": "h",
                "target": 1
            }],
            "results": [{
                "type": "expectation",
                "observable": ["x"],
                "targets": [1]
            }],
        }))
    shots_count = 1000
    result = simulator.run(ir, qubit_count=2, shots=shots_count)
    assert all([len(measurement) == 2] for measurement in result.measurements)
    assert len(result.measurements) == shots_count
    assert not result.resultTypes
    assert result.measuredQubits == [0, 1]
def test_simulator_run_statevector_shots():
    simulator = DefaultSimulator()
    ir = Program.parse_raw(
        json.dumps({
            "instructions": [{
                "type": "h",
                "target": 0
            }],
            "results": [{
                "type": "statevector"
            }]
        }))
    simulator.run(ir, qubit_count=2, shots=100)
def test_simulator_run_amplitude_no_shots_invalid_states():
    simulator = DefaultSimulator()
    ir = Program.parse_raw(
        json.dumps({
            "instructions": [{
                "type": "h",
                "target": 0
            }],
            "results": [{
                "type": "amplitude",
                "states": ["0"]
            }],
        }))
    simulator.run(ir, qubit_count=2, shots=0)
def bell_ir():
    return Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ]
        }))
def test_simulator_fails_samples_0_shots():
    simulator = DefaultSimulator()
    prog = Program.parse_raw(
        json.dumps({
            "instructions": [{
                "type": "h",
                "target": 0
            }],
            "results": [{
                "type": "sample",
                "observable": ["x"],
                "targets": [0]
            }],
        }))
    simulator.run(prog, qubit_count=1, shots=0)
Exemplo n.º 8
0
    def to_ir(self) -> Program:
        """
        Converts the circuit into the canonical intermediate representation.
        If the circuit is sent over the wire, this method is called before it is sent.

        Returns:
            (Program): An AWS quantum circuit description program in JSON format.
        """
        ir_instructions = [instr.to_ir() for instr in self.instructions]
        ir_results = [result_type.to_ir() for result_type in self.result_types]
        ir_basis_rotation_instructions = [
            instr.to_ir() for instr in self.basis_rotation_instructions
        ]
        return Program.construct(
            instructions=ir_instructions,
            results=ir_results,
            basis_rotation_instructions=ir_basis_rotation_instructions,
        )
def test_simulator_fails_overlapping_targets_different_observable(
        result_types):
    simulator = DefaultSimulator()
    prog = Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ],
            "results":
            result_types,
        }))
    simulator.run(prog, qubit_count=2, shots=0)
def test_simulator_identity():
    simulator = DefaultSimulator()
    shots_count = 1000
    result = simulator.run(
        Program.parse_raw(
            json.dumps({
                "instructions": [{
                    "type": "i",
                    "target": 0
                }, {
                    "type": "i",
                    "target": 1
                }]
            })),
        qubit_count=2,
        shots=shots_count,
    )
    counter = Counter(
        ["".join(measurement) for measurement in result.measurements])
    assert counter.keys() == {"00"}
    assert counter["00"] == shots_count
def test_simulator_accepts_overlapping_targets_same_observable(
        result_types, expected_expectation, expected_variance):
    simulator = DefaultSimulator()
    prog = Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ],
            "results":
            result_types,
        }))
    result = simulator.run(prog, qubit_count=2, shots=0)
    expectation = result.resultTypes[0].value
    variance = result.resultTypes[1].value
    assert np.allclose(expectation, expected_expectation)
    assert np.allclose(variance, expected_variance)
def grcs_16_qubit():
    with open("test/resources/grcs_16.json") as circuit_file:
        data = json.load(circuit_file)
        return CircuitData(Program.parse_raw(json.dumps(data["ir"])),
                           data["probability_zero"])
                "targets": [1]
            }],
        }))
    shots_count = 1000
    simulator.run(ir, qubit_count=2, shots=shots_count)


@pytest.mark.parametrize(
    "ir, qubit_count",
    [
        (
            Program.parse_raw(
                json.dumps({
                    "instructions": [{
                        "type": "z",
                        "target": 2
                    }],
                    "basis_rotation_instructions": [],
                    "results": [],
                })),
            1,
        ),
        (
            Program.parse_raw(
                json.dumps({
                    "instructions": [{
                        "type": "h",
                        "target": 0
                    }],
                    "basis_rotation_instructions": [{
                        "type": "z",
Exemplo n.º 14
0
def test_result_in_program(testclass, subclasses, type):
    result = create_valid_class_instance(testclass, subclasses, type)
    program = Program(instructions=[], results=[result])
    assert program.results == [result]
Exemplo n.º 15
0
def test_instruction_in_program(testclass, subclasses, type):
    instruction = create_valid_class_instance(testclass, subclasses, type)
    program = Program(instructions=[instruction])
    assert program.instructions == [instruction]
Exemplo n.º 16
0
def program():
    return Program(instructions=[CNot(control=0, target=1)])