def test_to_resolvers_single(): resolver = cirq.ParamResolver({}) assert list(cirq.to_resolvers(resolver)) == [resolver] assert list(cirq.to_resolvers({})) == [resolver]
def test_to_resolvers_iterable(): resolvers = [cirq.ParamResolver({'a': 2}), cirq.ParamResolver({'a': 1})] assert list(cirq.to_resolvers(resolvers)) == resolvers assert list(cirq.to_resolvers([{'a': 2}, {'a': 1}])) == resolvers
def test_to_sweep_single_resolver(r_gen): sweep = cirq.to_sweep(r_gen()) assert isinstance(sweep, cirq.Sweep) assert list(sweep) == [cirq.ParamResolver({'a': 1})]
def test_to_resolvers_none(): assert list(cirq.to_resolvers(None)) == [cirq.ParamResolver({})]
def test_to_sweep_sweep(): sweep = cirq.Linspace('a', 0, 1, 10) assert cirq.to_sweep(sweep) is sweep @pytest.mark.parametrize( 'r_gen', [ lambda: { 'a': 1 }, lambda: { sympy.Symbol('a'): 1 }, lambda: cirq.ParamResolver({'a': 1}), lambda: cirq.ParamResolver({sympy.Symbol('a'): 1}), ], ) def test_to_sweep_single_resolver(r_gen): sweep = cirq.to_sweep(r_gen()) assert isinstance(sweep, cirq.Sweep) assert list(sweep) == [cirq.ParamResolver({'a': 1})] @pytest.mark.parametrize( 'r_list_gen', [ # Lists lambda: [{ 'a': 1
a, b = sympy.symbols('a b') # 2qubits circuit q0, q1 = cirq.GridQubit.rect(1, 2) # rx gate on q0 with parameter a, and ry gate on q1 with parameter b circuit = cirq.Circuit( cirq.rx(a).on(q0), cirq.ry(b).on(q1), cirq.CNOT(control=q0, target=q1)) SVGCircuit(circuit) # state vector at a=0.5, b=-0.5 resolver = cirq.ParamResolver({a: 0.5, b: -0.5}) output_state_vector = cirq.Simulator().simulate(circuit, resolver).final_state output_state_vector #gets an array([ 0.9387913 +0.j , -0.23971277+0.j , 0. +0.06120872j, 0. -0.23971277j], dtype=complex64) z0 = cirq.Z(q0) qubit_map={q0: 0, q1: 1} z0.expectation_from_wavefunction(output_state_vector, qubit_map).real # result 0.8775825500488281
def test_simulate_sweep_parameters_not_resolved(): a = cirq.LineQubit(0) simulator = cirq.DensityMatrixSimulator() circuit = cirq.Circuit(cirq.XPowGate(exponent=sympy.Symbol('a'))(a), cirq.measure(a)) with pytest.raises(ValueError, match='symbols were not specified'): _ = simulator.simulate_sweep(circuit, cirq.ParamResolver({}))
def f(x): # Create circuit betas = x[:p] betas_dict = {'beta' + str(index): betas[index] for index in range(p)} gammas = x[p:] gammas_dict = { 'gamma' + str(index): gammas[index] for index in range(p) } param_resolver = cirq.ParamResolver({**betas_dict, **gammas_dict}) # VALIDATE STATE VECTOR SIMULATION # qsim_circuit = cirq.resolve_parameters(cirq_circuit, param_resolver) # qsim_circuit_01 = qsimcirq.QSimCircuit(cirq.resolve_parameters(meas_circuit, param_resolver)) # qsim_circuit_04 = qsimcirq.QSimCircuit(cirq.resolve_parameters(meas_circuit, param_resolver)) # qsim_circuit_16 = qsimcirq.QSimCircuit(cirq.resolve_parameters(meas_circuit, param_resolver)) # sv_sim_start = time.time() # sv_sim_result = sv_sim.simulate(cirq_circuit, param_resolver=param_resolver) # sv_sim_time = time.time() - sv_sim_start qs_sim_start = time.time() qs_sim_result = qs_sim_16.simulate(cirq_circuit, param_resolver=param_resolver) qs_sim_time = time.time() - qs_sim_start # kc_sim_start = time.time() # kc_sim_result = kc_sim.simulate(cirq_circuit, param_resolver=param_resolver) # kc_sim_time = time.time() - kc_sim_start # print("kc_sim_result.state_vector()=") # print(kc_sim_result.state_vector()) # print("qs_sim_result.state_vector()=") # print(qs_sim_result.state_vector()) # assert qs_sim_result.state_vector().shape == (1<<n,) # assert cirq.linalg.allclose_up_to_global_phase( # sv_sim_result.state_vector(), # qs_sim_result.state_vector(), # rtol = 1.e-5, # atol = 1.e-7, # ) # assert cirq.linalg.allclose_up_to_global_phase( # qs_sim_result.state_vector(), # kc_sim_result.state_vector(), # rtol = 1.e-4, # atol = 1.e-6, # ) # VALIDATE SAMPLING HISTOGRAMS # Sample bitstrings from circuit # sv_smp_start = time.time() # sv_smp_result = sv_sim.run(meas_circuit, param_resolver=param_resolver, repetitions=repetitions) # sv_smp_time = time.time() - sv_smp_start # sv_bitstrings = sv_smp_result.measurements['m'] # Process histogram # sv_histogram = defaultdict(int) # for bitstring in sv_bitstrings: # integer = 0 # for pos, bit in enumerate(reversed(bitstring)): # integer += bit<<pos # sv_histogram[integer] += 1 # Process bitstrings # nonlocal sv_largest_cut_found # nonlocal sv_largest_cut_value_found # sv_values = cut_values(sv_bitstrings, graph) # sv_max_value_index = np.argmax(sv_values) # sv_max_value = sv_values[sv_max_value_index] # if sv_max_value > sv_largest_cut_value_found: # sv_largest_cut_value_found = sv_max_value # sv_largest_cut_found = sv_bitstrings[sv_max_value_index] # sv_mean = np.mean(sv_values) # Sample bitstrings from circuit qs_smp_01_start = time.time() qs_smp_01_result = qs_sim_01.run(meas_circuit, param_resolver=param_resolver, repetitions=repetitions) qs_smp_01_time = time.time() - qs_smp_01_start qs_smp_01_time_dict[n].append(qs_smp_01_time) # qs_smp_02_start = time.time() # qs_smp_02_result = qs_sim_02.run(qsim_circuit_02, repetitions=repetitions) # qs_smp_02_time = time.time() - qs_smp_02_start # qs_smp_02_time_dict[n].append(qs_smp_02_time) qs_smp_04_start = time.time() qs_smp_04_result = qs_sim_04.run(meas_circuit, param_resolver=param_resolver, repetitions=repetitions) qs_smp_04_time = time.time() - qs_smp_04_start qs_smp_04_time_dict[n].append(qs_smp_04_time) # qs_smp_08_start = time.time() # qs_smp_08_result = qs_sim_08.run(qsim_circuit_08, repetitions=repetitions) # qs_smp_08_time = time.time() - qs_smp_08_start # qs_smp_08_time_dict[n].append(qs_smp_08_time) qs_smp_16_start = time.time() qs_smp_16_result = qs_sim_16.run(meas_circuit, param_resolver=param_resolver, repetitions=repetitions) qs_smp_16_time = time.time() - qs_smp_16_start qs_smp_16_time_dict[n].append(qs_smp_16_time) qs_bitstrings = qs_smp_16_result.measurements['m'] # Process histogram qs_histogram = defaultdict(int) # qs_bitstring_strs = [] for bitstring in qs_bitstrings: integer = 0 # string = '' for pos, bit in enumerate(bitstring): integer += bit << pos # string += str(bit) qs_histogram[integer] += 1 # qs_bitstring_strs.append(string) # Process bitstrings nonlocal qs_largest_cut_found nonlocal qs_largest_cut_value_found qs_values = cut_values( np.array([np.flip(qs_bitstring) for qs_bitstring in qs_bitstrings]), graph) qs_max_value_index = np.argmax(qs_values) qs_max_value = qs_values[qs_max_value_index] if qs_max_value > qs_largest_cut_value_found: qs_largest_cut_value_found = qs_max_value qs_largest_cut_found = qs_bitstrings[qs_max_value_index] qs_mean = np.mean(qs_values) # qh_smp_16_start = time.time() # qh_smp_16_result = qh_sim_16.compute_amplitudes(program=qsim_circuit, bitstrings=qs_bitstring_strs) # qh_smp_16_time = time.time() - qh_smp_16_start # qh_smp_16_time_dict[n].append(qh_smp_16_time) # Sample bitstrings from circuit kc_smp_start = time.time() kc_smp_result = kc_smp.run(meas_circuit, param_resolver=param_resolver, repetitions=repetitions) kc_smp_time = time.time() - kc_smp_start kc_smp_time_dict[n].append(kc_smp_time) # Process histogram kc_bitstrings = kc_smp_result.measurements['m'] kc_histogram = defaultdict(int) for bitstring in kc_bitstrings: integer = 0 for pos, bit in enumerate(reversed(bitstring)): integer += bit << pos kc_histogram[integer] += 1 # Process bitstrings nonlocal kc_largest_cut_found nonlocal kc_largest_cut_value_found kc_values = cut_values(kc_bitstrings, graph) kc_max_value_index = np.argmax(kc_values) kc_max_value = kc_values[kc_max_value_index] if kc_max_value > kc_largest_cut_value_found: kc_largest_cut_value_found = kc_max_value kc_largest_cut_found = kc_bitstrings[kc_max_value_index] kc_mean = np.mean(kc_values) # nonlocal iter # PRINT HISTOGRAMS # print ('iter,index,bitstring,bitstring_bin,sv_probability,sv_samples,qs_samples,kc_samples') # probabilities = np.zeros(1<<n) # for bitstring, amplitude in enumerate(sv_sim_result.state_vector()): # probability = abs(amplitude) * abs(amplitude) # probabilities[bitstring]=probability # sorted_bitstrings = np.argsort(probabilities) # for index, bitstring in enumerate(sorted_bitstrings): # print (str(iter)+','+str(index)+','+str(bitstring)+','+format(bitstring,'b').zfill(n)+','+str(probabilities[bitstring])+','+"{:.6e}".format(sv_histogram[bitstring]/repetitions)+','+"{:.6e}".format(qs_histogram[bitstring]/repetitions)+','+"{:.6e}".format(kc_histogram[bitstring]/repetitions)) print('qs_mean=' + str(qs_mean) + ' kc_mean=' + str(kc_mean)) # print ( 'sv_sim_time='+str(sv_sim_time)+' qs_sim_time='+str(qs_sim_time)+' kc_sim_time='+str(kc_sim_time) ) print('qs_smp_01_time=' + str(qs_smp_01_time) + ' qs_smp_04_time=' + str(qs_smp_04_time) + ' qs_smp_16_time=' + str(qs_smp_16_time) + ' kc_smp_time=' + str(kc_smp_time)) print( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') # iter += 1 return -qs_mean
def test_resolve_parameters(): assert cirq.resolve_parameters(CExpZinGate(sympy.Symbol('a')), cirq.ParamResolver({'a': 0.5})) == CExpZinGate(0.5) assert cirq.resolve_parameters(CExpZinGate(0.25), cirq.ParamResolver({})) == CExpZinGate(0.25)