def test_simulation_with_fsim_gates(x): """ Is a cirq Simulation of a cirq.Circuit consisting of fSim gates equal to the simulation through qFlex? :return: """ # Get configuration as a string final_conf = bin(x)[2:].zfill(len(qqubits)) options = { 'circuit': circuit_test_fsim.split('\n'), 'ordering': ordering_2x2_test.split('\n'), 'grid': grid_2x2_test.split('\n'), 'final_state': final_conf } # Pybind: Get output from qFlex qflex_amplitude1 = qflex.simulate(options)[0][1] # Cirq: Get output from qFlex sim = qflexcirq.QFlexSimulator() qflex_amplitude2 = sim.compute_amplitudes(qcir, bitstrings=[final_conf]) # Compare the amplitudes assert (np.abs(results_fsim.final_state[x] - qflex_amplitude1) < 1.e-6) # Compare the amplitudes assert (np.abs(results_fsim.final_state[x] - qflex_amplitude2) < 1.e-6)
def compute_amplitudes_sweep( self, program: circuits.Circuit, bitstrings: Sequence[int], params: study.Sweepable, qubit_order: ops.QubitOrderOrList = ops.QubitOrder.DEFAULT, ) -> Sequence[Sequence[complex]]: if not isinstance(program, qcirc.QFlexCircuit): raise ValueError('{!r} is not a QFlexCircuit'.format(program)) if not isinstance(program.device, qdevice.QFlexVirtualDevice): # The circuit was not validated against the device # TODO: Make it compatible? Validate, but for which grid? raise ValueError('{!r} is not a QFlexVirtualDevice'.format( program.device)) param_resolvers = study.to_resolvers(params) trials_results = [] for prs in param_resolvers: from cirq import protocols # The QFlexVirtualDevice device is "inherited" from the original program solved_circuit = protocols.resolve_parameters(program, prs) sweep_return = [] # Not sure what these params could look like...for the moment for bitstring in bitstrings: options = { 'circuit_filename': solved_circuit.circuit_data, 'ordering_filename': solved_circuit.ordering_data, 'grid_filename': program.device.grid_data, 'final_state': bitstring } amplitudes = qflex.simulate(options) for amp in amplitudes: amplitude = complex(amp[1]) # For debugging purposes commented the following # state = amp[0] # print(input_initial_state + " --> " + state + ": " + \ # str(amplitude.real) + " " + str(amplitude.imag)) # the amplitude for bitstring is stored sweep_return.append(amplitude) trials_results.append(sweep_return) return trials_results
def test_memory_limit(m): # Get configuration as a string final_conf = '0' * len(qubits) options = { 'circuit': circuit_test.split('\n'), 'ordering': ordering_test.split('\n'), 'grid': grid_test.split('\n'), 'final_state': final_conf, 'memory_limit': m } # Get output from qFlex qflex_amplitude = qflex.simulate(options)[0][1]
def test_simulation_with_auto_order(x): # Get configuration as a string final_conf = bin(x)[2:].zfill(len(qubits)) options = { 'circuit': circuit_test.split('\n'), 'ordering': auto_ordering, 'grid': grid_test.split('\n'), 'final_state': final_conf } # Get output from qFlex qflex_amplitude = qflex.simulate(options)[0][1] # Compare the amplitudes assert (np.abs(results.final_state[x] - qflex_amplitude) < 1.e-6)
def run_pybind_interface(config): print("\nRunning Pybind Interface\n") print(qflex.simulate(config))