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)
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",