Getting 'Figure(..x..)' when drawing quantum circuit with qiskit “mpl” output mode - quantum-computing

I am just starting with Quantum Computing and I tried the following code.
from qiskit import *
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
Qc = QuantumCircuit(qr,cr)
print ('This is the initial state')
print(Qc.draw(output = 'mpl'))
print ('')
The problem I am facing is with drawing the circuit. When use qc.draw() it gives me a line representation of the circuit and its good enough for me to see what is on it. But when I put output='mpl' I am getting the following results.
This results in
This is the initial state
Figure(142.949x204.68)
I have been using spyder for all my programming but I also tried the same thing in jupyter and the result was the same. What is this Figure(.............) and where can I find it?

When you use 'mpl', the output is a figure and not a string that you can print. Remove print and try:
print ('This is the initial state')
Qc.draw('mpl').show()
The output is:
This is the initial state

Hey I tried this with a simple change in the draw command
from qiskit import *
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
Qc = QuantumCircuit(qr,cr)
print ('This is the initial state')
Qc.draw(output = 'mpl')
Output:
If this didn't work try installing visualization tools

Related

Maya Python Mash - How can I add a selection set using Maya Mash API (in python)

Adding the cmds.connectAttr at the end does connect the selection set in Maya in the UI, but that's all it does. It acts as it its not registering.
import maya.cmds as cmds
import MASH.api as mapi
sel = cmds.ls(sl=1, l=1, fl=1)
new_set = cmds.sets(sel, n="custom_set")
obj_name = sel[0].split(".")[0]
shape = cmds.listRelatives(obj_name, s=True, f=1)
print(shape)
shape = "pCylinderShape1" #distribute mesh
cmds.select("|pCylinder2") #main mesh MASH
#create a new MASH network
mashNetwork = mapi.Network()
mashNetwork.createNetwork(name="Custom_placement", geometry="Repro")
shape = "pCylinderShape1"
mashNetwork.meshDistribute(shape, 4)
cmds.connectAttr(new_set+".message", mashNetwork.distribute+".selectionSetMessage")
Closest answer I found was here but I am not a programmer to know what that means.
If anyone can help, I'd much appreciate it.
After a lot of investigation I did manage to find the answer from the provided link.
The code I wrote was only the first part of the solution.
hasMASHFlag = cmds.objExists('%s.mashOutFilter' % (new_set))
if not hasMASHFlag:
cmds.addAttr( new_set, longName='mashOutFilter', attributeType='bool' )
cmds.setAttr( '%s.arrangement' % (mashNetwork.distribute), 4 )
cmds.setAttr( '%s.meshType' % (mashNetwork.distribute), 7 )
Note sure is needed but I noticed when you connect a selection set by default it has an extra attribute called mashOutFilter.
The part that is needed is the .meshType that makes it work.

Repeating Processes on python but with mixed variables

Currently im trying to finish a script to pass links from a youtube playlist trough youtube_dl to be able to stream the audio from them on to a player on my raspberry pi, but I don't know how to repeat a process but changing the variable automatically without changing every single number on the array like so:
import os
import pafy
import youtube_dl as yt
from youtube_dl import YoutubeDL
links = ["http://www.youtube.com/watch?v=JaSfjAIcGpQ",
"http://www.youtube.com/watch?v=pzAo3Hj15R4",
"http://www.youtube.com/watch?v=hWdovALEen0",
"http://www.youtube.com/watch?v=JVpTp8IHdEg",
"http://www.youtube.com/watch?v=Pw-0pbY9JeU",
"http://www.youtube.com/watch?v=lrfhf1Gv4Tw",
"http://www.youtube.com/watch?v=ilw-qmqZ5zY",
"http://www.youtube.com/watch?v=UwsY_foobEw",
"http://www.youtube.com/watch?v=RSNmgE6L8AU",
"http://www.youtube.com/watch?v=d020hcWA_Wg",
"http://www.youtube.com/watch?v=O4irXQhgMqg",
"http://www.youtube.com/watch?v=9P16xvwMQ5A",
"http://www.youtube.com/watch?v=fregObNcHC8",
"http://www.youtube.com/watch?v=fLN6ec7-43s",
"http://www.youtube.com/watch?v=DcHKOC64KnE",
"http://www.youtube.com/watch?v=UYwF-jdcVjY",
"http://www.youtube.com/watch?v=jQcBwE6j09U",
"http://www.youtube.com/watch?v=lXgkuM2NhYI",
"http://www.youtube.com/watch?v=vabnZ9-ex7o",
"http://www.youtube.com/watch?v=neNJvLIKaOk",
"http://www.youtube.com/watch?v=k4V3Mo61fJM",
"http://www.youtube.com/watch?v=pstVCGyaUBM",
"http://www.youtube.com/watch?v=D8Ymd-OCucs",
"http://www.youtube.com/watch?v=SeRJb3U1v3A",
"http://www.youtube.com/watch?v=J0DjcsK_-HY",
"http://www.youtube.com/watch?v=f2JuxM-snGc",
"http://www.youtube.com/watch?v=Eyjj8BgsBGU",
"http://www.youtube.com/watch?v=dX3k_QDnzHE",
"http://www.youtube.com/watch?v=luM6oeCM7Yw",
"https://www.youtube.com/watch?v=5iC0YXspJRM",
"https://www.youtube.com/watch?v=BnOVufgbIzA",
"https://www.youtube.com/watch?v=wwK_Kmh0P90",
"https://www.youtube.com/watch?v=UYfh9YhUVdE"]
y = YoutubeDL({
'format': 'bestaudio',
})
url = links[0]
r = y.extract_info(url, download=False)
print(r['ext'])
print(r['url'])
I am trying to change the part of "links[0]"
so i can repeat this process but without changing the number manually, but instead doing it in python. Im still new at python.
As I understand you want to iterate over the list. You can make it with a for loop.
Like so:
for link in links:
url = link
r = y.extract_info(url, download=False)
Answer above seems okay, just to add on other ways to do it:
Shortest version:
extracted_details_list = [y.extract_info(url, download=False) for url in links] # list comprehension
A long version, could be easier to get the flow of for-loops:
for i in range(len(links)):
url = links[i]
r = y.extract_info(url, download=False)
print(r['ext'])
print(r['url']

Chef - Ruby - How to extract values from a nested array/list

I'm using this json content (I'm open to suggestions on better formatting here):
{"forwardingZones": [{"name": "corp.mycompany.com","dnsServers": ["192.168.0.1","192.168.0.2"]}]}
Note: we may add more items to this list as we scale out, both more IPs and more names, hence the "join(',')" in the end of the code below.
And I'm trying to loop through it to get this result:
corp.mycompany.com=192.168.0.1;192.168.0.2
Using this code:
forward_zones = node['DNS']['forward_zones'].each do |forwarded_zone|
forwarded_zone_name = forwarded_zone['name']
forwarded_zone_dns_servers = forwarded_zone['dns_servers'].join(';')
"#{forwarded_zone_name}=#{forwarded_zone_dns_servers}"
end.join(',')
This is the result that I get:
{"dnsServers"=>["192.168.0.1", "192.168.0.2"], "name"=>"corp.mycompany.com"}
What am i doing wrong...?
x.each returns x. You want x.map.

How do I convert binary field to text in Ruby?

I have a MSSQL table with a field of type image that has some text stored in it.
The field has data that looks like this:
54004800490053002000490053002000410020004c00490047004800540041005200540020004f0052004400450052002e00200020004c004900470048005400410052005400200049005300200044004f0049004e004700200054004800450020004600410042002e000d000a004c00490047004800540041005200540020005300480049005000500049004e004700200054004f00200043005500530054004f004d004500520020003c0038002d00320033002d00310037003e000d000a000d000a0043006f006e006e00690065002c00200070006c00650061007300650020007000720069006e007400200073007400690063006b00650072007300200066006f0072002000650061006300680020006f007500740065007200200062006f00780020007400680061007400200069006e0063006c0075006400650073002000740068006500200069006e0066006f003a000d000a0028003100290020006f00660020002800310029000d000a004c004100320020005400680072006500650020004c00610072006700650020000d000a00380036005c0022004c0020007800200036005c002200570020007800200038005c00220048000d000a004e00610074007500720061006c000d000a005000320030003900380031003000350020004d004f0044002000500069007a007a00610020005300750067006100720068006f007500730065002c00200055005400
In PHP I can write a SQL query to convert that data like this: SELECT CAST(CAST(CUST_ORDER_BINARY.BITS as VARBINARY(8000)) as VARCHAR(8000)) as result FROM CUST_ORDER_BINARY WHERE CUST_ORDER_ID = 'CO-299403S';
When I try the same thing in Ruby I get a result like this:
specs = VisualCustomer.connection.exec_query(sql).first
{"result"=>"T\u0000H\u0000I\u0000S\u0000 \u0000I\u0000S\u0000 \u0000A\u0000 \u0000L\u0000I\u0000G\u0000H\u0000T\u0000A\u0000R\u0000T\u0000 \u0000O\u0000R\u0000D\u0000E\u0000R\u0000.\u0000 \u0000 \u0000L\u0000I\u0000G\u0000H\u0000T\u0000A\u0000R\u0000T\u0000 \u0000I\u0000S\u0000 \u0000D\u0000O\u0000I\u0000N\u0000G\u0000 \u0000T\u0000H\u0000E\u0000 \u0000F\u0000A\u0000B\u0000.\u0000\r\u0000\n\u0000L\u0000I\u0000G\u0000H\u0000T\u0000A\u0000R\u0000T\u0000 \u0000S\u0000H\u0000I\u0000P\u0000P\u0000I\u0000N\u0000G\u0000 \u0000T\u0000O\u0000 \u0000C\u0000U\u0000S\u0000T\u0000O\u0000M\u0000E\u0000R\u0000 \u0000<\u00008\u0000-\u00002\u00003\u0000-\u00001\u00007\u0000>\u0000\r\u0000\n\u0000\r\u0000\n\u0000C\u0000o\u0000n\u0000n\u0000i\u0000e\u0000,\u0000 \u0000p\u0000l\u0000e\u0000a\u0000s\u0000e\u0000 \u0000p\u0000r\u0000i\u0000n\u0000t\u0000 \u0000s\u0000t\u0000i\u0000c\u0000k\u0000e\u0000r\u0000s\u0000 \u0000f\u0000o\u0000r\u0000 \u0000e\u0000a\u0000c\u0000h\u0000 \u0000o\u0000u\u0000t\u0000e\u0000r\u0000 \u0000b\u0000o\u0000x\u0000 \u0000t\u0000h\u0000a\u0000t\u0000 \u0000i\u0000n\u0000c\u0000l\u0000u\u0000d\u0000e\u0000s\u0000 \u0000t\u0000h\u0000e\u0000 \u0000i\u0000n\u0000f\u0000o\u0000:\u0000\r\u0000\n\u0000(\u00001\u0000)\u0000 \u0000o\u0000f\u0000 \u0000(\u00001\u0000)\u0000\r\u0000\n\u0000L\u0000A\u00002\u0000 \u0000T\u0000h\u0000r\u0000e\u0000e\u0000 \u0000L\u0000a\u0000r\u0000g\u0000e\u0000 \u0000\r\u0000\n\u00008\u00006\u0000\\\u0000\"\u0000L\u0000 \u0000x\u0000 \u00006\u0000\\\u0000\"\u0000W\u0000 \u0000x\u0000 \u00008\u0000\\\u0000\"\u0000H\u0000\r\u0000\n\u0000N\u0000a\u0000t\u0000u\u0000r\u0000a\u0000l\u0000\r\u0000\n\u0000P\u00002\u00000\u00009\u00008\u00001\u00000\u00005\u0000 \u0000M\u0000O\u0000D\u0000 \u0000P\u0000i\u0000z\u0000z\u0000a\u0000 \u0000S\u0000u\u0000g\u0000a\u0000r\u0000h\u0000o\u0000u\u0000s\u0000e\u0000,\u0000 \u0000U\u0000T\u0000"}
So the data is "almost" there. :)
I've tried gsubing to remove the \u0000 from the result but that's not working, obviously.
** EDIT 1 **
So, for some reason, getting the data from MSSQL into ruby is causing some kind of partial translation. I never get the raw data from the field, instead I get the "semi-translated" data. Even if I just query it, it still comes out like
"T\x00H\x00I\x00S\x00 \x00I\x00S\x00 \x00A\x00...
I tried to put it back doing:
s = order_specs.each_byte.map { |b| b.to_s(16) }.join
Then, when I do:
order_specs = s.scan(/.{2}(?=0{2})/).map{|s| s.to_i(16)}.pack("c*").tr("\x02", " ")
I just get an empty string. :/
That happens when you're inspecting the data, but when you write it will be fine:
Example:
$ ruby -e 'bin = File.read("/bin/ls");p bin; File.open("/tmp/file","w+"){|f| f.write bin}'
"\u007FELF\u0002\u0001\u0001\u0000\u0000\u0000 ...
....
$ md5sum /bin/ls
84b7b042405dfc79f2afe9b12d6b931d /bin/ls
$ md5sum /tmp/file
84b7b042405dfc79f2afe9b12d6b931d /tmp/file
So here we read a binary file /bin/ls and wrote it to another file /tmp/file as you can the the checksums are the identical.
s = "54004800490053002000490053002000410020004c00490047004800540041005200540020004f0052004400450052002e00200020004c004900470048005400410052005400200049005300200044004f0049004e004700200054004800450020004600410042002e000d000a004c00490047004800540041005200540020005300480049005000500049004e004700200054004f00200043005500530054004f004d004500520020003c0038002d00320033002d00310037003e000d000a000d000a0043006f006e006e00690065002c00200070006c00650061007300650020007000720069006e007400200073007400690063006b00650072007300200066006f0072002000650061006300680020006f007500740065007200200062006f00780020007400680061007400200069006e0063006c0075006400650073002000740068006500200069006e0066006f003a000d000a0028003100290020006f00660020002800310029000d000a004c004100320020005400680072006500650020004c00610072006700650020000d000a00380036005c0022004c0020007800200036005c002200570020007800200038005c00220048000d000a004e00610074007500720061006c000d000a005000320030003900380031003000350020004d004f0044002000500069007a007a00610020005300750067006100720068006f007500730065002c00200055005400"
Code:
puts s.scan(/.{2}(?=0{2})/).map{|s| s.to_i(16)}.pack("c*")
Output:
THISISALIGHTARTORDER.LIGHTARTISDOINGTHEFAB.
LIGHTARTSHIINGTOCUSTOMER<8-23-17>
Connie,leaserintstickersforeachouterboxthatincludestheinfo:
(1)of(1)
LA2ThreeLarge
86\"Lx6\"Wx8\"H
Natural
29815MODizzaSugarhouse,UT
Note: Some characters are unprintable, so they do not appear in this page. See the edit page of this answer for detail.
Or, if you replace "\x02" with a space,
puts s.scan(/.{2}(?=0{2})/).map{|s| s.to_i(16)}.pack("c*").tr("\x02", " ")
you get:
THIS IS A LIGHTART ORDER. LIGHTART IS DOING THE FAB.
LIGHTART SHIING TO CUSTOMER <8-23-17>
Connie, lease rint stickers for each outer box that includes the info:
(1) of (1)
LA2 Three Large
86\"L x 6\"W x 8\"H
Natural
29815 MOD izza Sugarhouse, UT
I finally figured this out. I needed to do string.gsub("\u0000", '')
So, I was getting the data from the MSSQL database correctly it seemed, but I that null byte was really throwing things off and was being sent to the front end where it was appearing on the page. i swear i tried gsubing before but for whatever reason it wasn't working. I tried it again now before when the response is being formed and it is now being sent correctly.

sendKeys(Keys.TAB) not working in JMeter Webdriver Sampler

I am trying to enter a value into a textfield then Tab to the next field (which also enters the value). The Keys.TAB method does not seem to be working.
My code is as follows:
var Keys = JavaImporter(org.openqa.selenium.Keys)
var input = WDS.browser.findElement(pkg.By.xpath('xpath_to_input'))
input.sendKeys('value')
input.sendKeys(Keys.TAB)
I am getting the following error:
sun.org.mozilla.javascript.internal.EvaluatorException: Can't find method org.openqa.selenium.remote.RemoteWebElement.sendKeys(string). <Unknown source>
Thank you for your help. I have tried all sorts of things and it will not work.
In addition to what ekuusela suggests there are 2 more options:
Use \t escape sequence like:
input.sendKeys('value\t');
Use java.awt.Robot approach as follows:
input.sendKeys('value')
var robot = new java.awt.Robot()
var keyEvent = java.awt.event.KeyEvent
robot.keyPress(keyEvent.VK_TAB)
robot.keyRelease(keyEvent.VK_TAB)
Remember that "Robot" approach simulates native key and mouse event on the machine where it is executed so if you use remote webdriver instance it won't play.
For more WebDriver Sampler tips and tricks see The WebDriver Sampler: Your Top 10 Questions Answered guide.
If you use Java 6 you must pass the string in an array, like this:
var input = WDS.browser.findElement(pkg.By.xpath('xpath_to_input'))
input.sendKeys(['value'])
input.sendKeys([Keys.TAB])
http://jmeter-plugins.org/wiki/WebDriverSampler/

Resources